Skip to content

Commit dc63ed4

Browse files
committed
windows: add GetKeyboardLayout & ToUnicodeEx
These are used along with GetForegroundWindow and GetWindowThreadProcessId to determine the current user layout and translate the base key the user has pressed.
1 parent 6943ab6 commit dc63ed4

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

windows/syscall_windows.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"unsafe"
1818
)
1919

20-
type Handle uintptr
21-
type HWND uintptr
20+
type (
21+
Handle uintptr
22+
HWND uintptr
23+
)
2224

2325
const (
2426
InvalidHandle = ^Handle(0)
@@ -211,6 +213,8 @@ func NewCallbackCDecl(fn interface{}) uintptr {
211213
//sys OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (handle Handle, err error)
212214
//sys ShellExecute(hwnd Handle, verb *uint16, file *uint16, args *uint16, cwd *uint16, showCmd int32) (err error) [failretval<=32] = shell32.ShellExecuteW
213215
//sys GetWindowThreadProcessId(hwnd HWND, pid *uint32) (tid uint32, err error) = user32.GetWindowThreadProcessId
216+
//sys GetKeyboardLayout(tid uint32) (hkl Handle) = user32.GetKeyboardLayout
217+
//sys toUnicodeEx(vkey uint32, scancode uint32, keystate *byte, pwszBuff *uint16, cchBuff int32, flags uint32, hkl Handle) (ret int32) = user32.ToUnicodeEx
214218
//sys GetShellWindow() (shellWindow HWND) = user32.GetShellWindow
215219
//sys MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW
216220
//sys ExitWindowsEx(flags uint32, reason uint32) (err error) = user32.ExitWindowsEx
@@ -1368,9 +1372,11 @@ func SetsockoptLinger(fd Handle, level, opt int, l *Linger) (err error) {
13681372
func SetsockoptInet4Addr(fd Handle, level, opt int, value [4]byte) (err error) {
13691373
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&value[0])), 4)
13701374
}
1375+
13711376
func SetsockoptIPMreq(fd Handle, level, opt int, mreq *IPMreq) (err error) {
13721377
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq)))
13731378
}
1379+
13741380
func SetsockoptIPv6Mreq(fd Handle, level, opt int, mreq *IPv6Mreq) (err error) {
13751381
return syscall.EWINDOWS
13761382
}
@@ -1916,3 +1922,13 @@ const (
19161922
EV_ERR = 0x0080
19171923
EV_RING = 0x0100
19181924
)
1925+
1926+
// ToUnicodeEx Translates the specified virtual-key code and keyboard state to
1927+
// the corresponding Unicode character or characters.
1928+
func ToUnicodeEx(virtualKey, scanCode uint32, buf []uint16, flags uint32, layout Handle) int32 {
1929+
if len(buf) == 0 {
1930+
return 0
1931+
}
1932+
var keyState [256]byte
1933+
return toUnicodeEx(virtualKey, scanCode, &keyState[0], &buf[0], int32(len(buf)), flags, layout)
1934+
}

windows/zsyscall_windows.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)