#Requires AutoHotkey v2.0

hook := DllCall("SetWindowsHookEx", "int", 13, "ptr", CallbackCreate(WindowsHookProc), "ptr", 0, "uint", 0, "ptr")
ui := Gui("AlwaysOnTop E0x8000000 -MaximizeBox -MinimizeBox")
ui.AddText(, "阻止物理输入,允许模拟输入")
btn := ui.AddButton(, "Send a")
btn.OnEvent("Click", (*) => Send("a"))
ui.OnEvent("Close", (*) => (DllCall("UnhookWindowsHookEx", "ptr", hook), 0))
ui.Show("NoActivate")

WindowsHookProc(nCode, wParam, lParam) {
  if 0 == nCode {
    info := PKBDLLHOOKSTRUCT(lParam)
    if info.flags & 0x10 {
      ToolTip "模拟输入 " GetKeyName(Format("vk{:02x}", info.vkCode))
    }
    else {
      ToolTip "物理输入 " GetKeyName(Format("vk{:02x}", info.vkCode))
      return 1 ; 阻止物理输入
    }
  }
  return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", wParam, "ptr", lParam, "ptr")
}

class PKBDLLHOOKSTRUCT {
  __New(lParam) => this.Ptr := lParam
  vkCode => NumGet(this.Ptr, "uint")
  scanCode => NumGet(this.Ptr, 4, "uint")
  flags => NumGet(this.Ptr, 8, "uint")
  time => NumGet(this.Ptr, 12, "uint")
  dwExtraInfo => NumGet(this.Ptr, 16, "ptr")
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。