Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions RegisterHotKey/UnregisterHotKey/GetCurrentProcessId #80

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Kevin Pors <krpors@gmail.com>
ktye <ktye@users.noreply.github.com>
mycaosf <mycaosf@gmail.com>
ryujimiya <ryujimiya236@gmail.com>
Shawn Silva <713367+shawnsilva@users.noreply.github.com>
Simon Rozman <simon@rozman.si>
Tiago Carvalho <sugoiuguu@tfwno.gf>
wsf01 <wf1337@sina.com>
11 changes: 11 additions & 0 deletions kernel32.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
findResource *windows.LazyProc
getConsoleTitle *windows.LazyProc
getConsoleWindow *windows.LazyProc
getCurrentProcessId *windows.LazyProc
getCurrentThreadId *windows.LazyProc
getLastError *windows.LazyProc
getLocaleInfo *windows.LazyProc
Expand Down Expand Up @@ -151,6 +152,7 @@ func init() {
findResource = libkernel32.NewProc("FindResourceW")
getConsoleTitle = libkernel32.NewProc("GetConsoleTitleW")
getConsoleWindow = libkernel32.NewProc("GetConsoleWindow")
getCurrentProcessId = libkernel32.NewProc("GetCurrentProcessId")
getCurrentThreadId = libkernel32.NewProc("GetCurrentThreadId")
getLastError = libkernel32.NewProc("GetLastError")
getLocaleInfo = libkernel32.NewProc("GetLocaleInfoW")
Expand Down Expand Up @@ -242,6 +244,15 @@ func GetConsoleWindow() HWND {
return HWND(ret)
}

func GetCurrentProcessId() uint32 {
ret, _, _ := syscall.Syscall(getCurrentProcessId.Addr(), 0,
0,
0,
0)

return uint32(ret)
}

func GetCurrentThreadId() uint32 {
ret, _, _ := syscall.Syscall(getCurrentThreadId.Addr(), 0,
0,
Expand Down
73 changes: 53 additions & 20 deletions user32.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ const (
UISF_ACTIVE = 0x4
)

// Key Modifiers
const (
MOD_ALT = 0x0001
MOD_CONTROL = 0x0002
MOD_SHIFT = 0x0004
MOD_WIN = 0x0008
)

// Virtual key codes
const (
VK_LBUTTON = 1
Expand Down Expand Up @@ -1758,8 +1766,8 @@ var (
// Functions
addClipboardFormatListener *windows.LazyProc
adjustWindowRect *windows.LazyProc
attachThreadInput *windows.LazyProc
animateWindow *windows.LazyProc
attachThreadInput *windows.LazyProc
beginDeferWindowPos *windows.LazyProc
beginPaint *windows.LazyProc
bringWindowToTop *windows.LazyProc
Expand Down Expand Up @@ -1857,6 +1865,7 @@ var (
postQuitMessage *windows.LazyProc
redrawWindow *windows.LazyProc
registerClassEx *windows.LazyProc
registerHotKey *windows.LazyProc
registerRawInputDevices *windows.LazyProc
registerWindowMessage *windows.LazyProc
releaseCapture *windows.LazyProc
Expand Down Expand Up @@ -1894,6 +1903,7 @@ var (
trackPopupMenuEx *windows.LazyProc
translateMessage *windows.LazyProc
unhookWinEvent *windows.LazyProc
unregisterHotKey *windows.LazyProc
updateWindow *windows.LazyProc
windowFromDC *windows.LazyProc
windowFromPoint *windows.LazyProc
Expand All @@ -1908,8 +1918,8 @@ func init() {
// Functions
addClipboardFormatListener = libuser32.NewProc("AddClipboardFormatListener")
adjustWindowRect = libuser32.NewProc("AdjustWindowRect")
attachThreadInput = libuser32.NewProc("AttachThreadInput")
animateWindow = libuser32.NewProc("AnimateWindow")
attachThreadInput = libuser32.NewProc("AttachThreadInput")
beginDeferWindowPos = libuser32.NewProc("BeginDeferWindowPos")
beginPaint = libuser32.NewProc("BeginPaint")
bringWindowToTop = libuser32.NewProc("BringWindowToTop")
Expand Down Expand Up @@ -2012,6 +2022,7 @@ func init() {
postQuitMessage = libuser32.NewProc("PostQuitMessage")
redrawWindow = libuser32.NewProc("RedrawWindow")
registerClassEx = libuser32.NewProc("RegisterClassExW")
registerHotKey = libuser32.NewProc("RegisterHotKey")
registerRawInputDevices = libuser32.NewProc("RegisterRawInputDevices")
registerWindowMessage = libuser32.NewProc("RegisterWindowMessageW")
releaseCapture = libuser32.NewProc("ReleaseCapture")
Expand Down Expand Up @@ -2054,6 +2065,7 @@ func init() {
trackPopupMenuEx = libuser32.NewProc("TrackPopupMenuEx")
translateMessage = libuser32.NewProc("TranslateMessage")
unhookWinEvent = libuser32.NewProc("UnhookWinEvent")
unregisterHotKey = libuser32.NewProc("UnregisterHotKey")
updateWindow = libuser32.NewProc("UpdateWindow")
windowFromDC = libuser32.NewProc("WindowFromDC")
windowFromPoint = libuser32.NewProc("WindowFromPoint")
Expand Down Expand Up @@ -2081,15 +2093,6 @@ func AdjustWindowRect(lpRect *RECT, dwStyle uint32, bMenu bool) bool {
return ret != 0
}

func AttachThreadInput(idAttach int32, idAttachTo int32, fAttach bool) bool {
ret, _, _ := syscall.Syscall(attachThreadInput.Addr(), 3,
uintptr(idAttach),
uintptr(idAttachTo),
uintptr(BoolToBOOL(fAttach)))

return ret != 0
}

func AnimateWindow(hwnd HWND, dwTime, dwFlags uint32) bool {
ret, _, _ := syscall.Syscall(animateWindow.Addr(), 3,
uintptr(hwnd),
Expand All @@ -2099,6 +2102,15 @@ func AnimateWindow(hwnd HWND, dwTime, dwFlags uint32) bool {
return ret != 0
}

func AttachThreadInput(idAttach uint32, idAttachTo uint32, fAttach bool) bool {
ret, _, _ := syscall.Syscall(attachThreadInput.Addr(), 3,
uintptr(idAttach),
uintptr(idAttachTo),
uintptr(BoolToBOOL(fAttach)))

return ret != 0
}

func BeginDeferWindowPos(nNumWindows int32) HDWP {
ret, _, _ := syscall.Syscall(beginDeferWindowPos.Addr(), 1,
uintptr(nNumWindows),
Expand All @@ -2108,15 +2120,6 @@ func BeginDeferWindowPos(nNumWindows int32) HDWP {
return HDWP(ret)
}

func GetWindowThreadProcessId(hwnd HWND, processId *uint32) uint32 {
ret, _, _ := syscall.Syscall(getWindowThreadProcessId.Addr(), 2,
uintptr(hwnd),
uintptr(unsafe.Pointer(processId)),
0)

return uint32(ret)
}

func BeginPaint(hwnd HWND, lpPaint *PAINTSTRUCT) HDC {
ret, _, _ := syscall.Syscall(beginPaint.Addr(), 2,
uintptr(hwnd),
Expand Down Expand Up @@ -2791,6 +2794,15 @@ func GetWindowRect(hWnd HWND, rect *RECT) bool {
return ret != 0
}

func GetWindowThreadProcessId(hWnd HWND, lpdwProcessId *uint32) uint32 {
ret, _, _ := syscall.Syscall(getWindowThreadProcessId.Addr(), 2,
uintptr(hWnd),
uintptr(unsafe.Pointer(lpdwProcessId)),
0)

return uint32(ret)
}

func InsertMenuItem(hMenu HMENU, uItem uint32, fByPosition bool, lpmii *MENUITEMINFO) bool {
ret, _, _ := syscall.Syscall6(insertMenuItem.Addr(), 4,
uintptr(hMenu),
Expand Down Expand Up @@ -3091,6 +3103,18 @@ func RegisterClassEx(windowClass *WNDCLASSEX) ATOM {
return ATOM(ret)
}

func RegisterHotKey(hwnd HWND, id int, fsModifiers, vk uint) bool {
ret, _, _ := syscall.Syscall6(registerHotKey.Addr(), 4,
uintptr(hwnd),
uintptr(id),
uintptr(fsModifiers),
uintptr(vk),
0,
0)

return ret != 0
}

func RegisterRawInputDevices(pRawInputDevices *RAWINPUTDEVICE, uiNumDevices uint32, cbSize uint32) bool {
ret, _, _ := syscall.Syscall(registerRawInputDevices.Addr(), 3,
uintptr(unsafe.Pointer(pRawInputDevices)),
Expand Down Expand Up @@ -3471,6 +3495,15 @@ func UnhookWinEvent(hWinHookEvent HWINEVENTHOOK) bool {
return ret != 0
}

func UnregisterHotKey(hwnd HWND, id int) bool {
ret, _, _ := syscall.Syscall(unregisterHotKey.Addr(), 2,
uintptr(hwnd),
uintptr(id),
0)

return ret != 0
}

func UpdateWindow(hwnd HWND) bool {
ret, _, _ := syscall.Syscall(updateWindow.Addr(), 1,
uintptr(hwnd),
Expand Down