Skip to content

Instantly share code, notes, and snippets.

@MasterGroosha
Created June 20, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterGroosha/a01141a0c07f21f9c97b0c15b64081bd to your computer and use it in GitHub Desktop.
Save MasterGroosha/a01141a0c07f21f9c97b0c15b64081bd to your computer and use it in GitHub Desktop.
#NoEnv
#SingleInstance force
SendMode Input
SetWorkingDir %A_ScriptDir%
#Include AHKHID.ahk
;Set up the constants
AHKHID_UseConstants()
; Creating a stub GUI
Gui, +LastFound
hGui := WinExist()
; Initiate allowWin variable
allowWin := 1
;Intercept WM_INPUT and register our keyboards
OnMessage(0x00FF, "InputMsg")
AHKHID_AddRegister(1)
AHKHID_AddRegister(1, 6, hGui, RIDEV_INPUTSINK)
AHKHID_Register()
; Pretty-print keyboard ID
GetDevKey(i, IsHandle = false) {
;generate a unique name from the DevName (which is huge, and usually includes a useless GUID)
devname := AHKHID_GetDevName(i, IsHandle)
StringReplace, devname, devname, Hid#Vid_, , All
StringReplace, devname, devname, USBVid_, , All
StringReplace, devname, devname, KODAK, , All
StringReplace, devname, devname, &Pid_, , All
StringReplace, devname, devname, &MI_, , All
StringReplace, devname, devname, #7&, , All
StringReplace, devname, devname, #, , All
StringReplace, devname, devname, &, , All
StringReplace, devname, devname, \, , All
StringReplace, devname, devname, ?, , All
p := Instr(devname, "{")
if p > 0
{
p := p - 1
StringLeft, devname, devname, p
}
;remove last 5 0's if that's what's there
if SubStr(devname, -4) = "00000"
{
p := StrLen(devname) - 5
StringLeft, devname, devname, p
}
StringUpper, devname, devname
Return devname
}
; Good is a keyboard for which I must have Win working
; Bad is a keyboard from which I must ignore Win button
Good := "046DC24800829B85094"
Bad := "045E0800007058F42"
InputMsg(wParam, lParam) {
Local r, h
Critical ;Or otherwise you could get ERROR_INVALID_HANDLE
d_handle := AHKHID_GetInputInfo(lParam, II_DEVHANDLE)
btn_id := AHKHID_GetInputInfo(lParam, II_KBD_VKEY)
device_name := GetDevKey(d_handle, True)
; Here we capture Win button from keyboards
if (device_name = Good and btn_id = 91) {
allowWin := 1
} else if (device_name = Bad and btn_id = 91) {
allowWin := 0
}
LWin::
msgbox, %allowWin% ; <-------------- THIS IS THE PROBLEM LINE!!!!!!!!!!!!!!!!!!!!
if (allowWin = 1) {
SendInput, {LWin}
} else if (allowWin = 0) {
Return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment