Coder Social home page Coder Social logo

Comments (2)

s77rt avatar s77rt commented on August 28, 2024 1

I have found a solution for that, but it have nothing to do with the windows api "keybd_event".
So i'm keeping the code here for future ref, hope it helps someone,
also i'm closing this issue for now.

Code: (Windows Only)

func PostKey(pid int, key uint8) (uintptr, error) {
	if runtime.GOOS == "windows" {
		var user32 = syscall.NewLazyDLL("user32.dll")

		EnumWindows := func (enumFunc uintptr, lparam uintptr) {
			user32.NewProc("EnumWindows").Call(uintptr(enumFunc), uintptr(lparam))
		}

		var hwnd uintptr
		cb := syscall.NewCallback(func(h uintptr , prm uintptr) uintptr {
			var itr_pid uint32
			itr_pid = 0x0001
			user32.NewProc("GetWindowThreadProcessId").Call(uintptr(h), uintptr(unsafe.Pointer(&itr_pid)))
			if int(itr_pid) == pid {
				hwnd = h
				user32.NewProc("PostMessageW").Call(h, 0x0100, uintptr(key), 0)
				//return 0 // stop enumeration (commented to make sure all windows created by our process get's the message)
			}
			return 1 // continue enumeration
		})
		EnumWindows(cb, 0)
		if hwnd == 0 {
			return 0, fmt.Errorf("No window with pid %d found", pid)
		}
		return hwnd, nil
	}
	return 0, fmt.Errorf("unsupported os")
}

Example:

PostKey(7777, 0x53) will send the key 'S' to all the windows that are created by the process which it's id is 7777
list of keys

Notes:

If you are using a pid = cmd.Process.Pid (os/exec)
your binary should be built passing the "-H=windowsgui" flag

from keybd_event.

micmonay avatar micmonay commented on August 28, 2024

The library simulates a keyboard and it has the same output with the hardware keyboard. If you have a solution to set multiple keyboards on various output, it is probably possible. But I don't know if that exists.

(The library does not specify the output)

from keybd_event.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.