如何在OSX上挂钩/重新映射任意键盘事件? [英] How to hook/remap an arbitrary keyboard event on OSX?

查看:92
本文介绍了如何在OSX上挂钩/重新映射任意键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想映射:

  • CAPS-LOCK Fn
  • Fn 鼠标左键
  • LSHIFT + 3 #
  • RSHIFT + 3 到其他内容
  • CAPS-LOCK to Fn
  • Fn to Left-MOUSE
  • LSHIFT+3 to #
  • RSHIFT+3 to something else
  • etc.

我已经详尽搜索了任何工具,这些工具可以完全自由地重新映射键盘输入,但是找不到. (Windows具有AutoHotkey).

I have searched exhaustively for any tool that offers complete freedom for remapping keyboard input, but cannot find one. (Windows has AutoHotkey).

我打算编写自己的工具来解析配置文件.

I'm planning to write my own tool that parses a config file.

但是如何真正挖掘并做到这一点呢?

But how to actually dig in and do this?

要解决此问题,需要彻底了解按键通过操作系统的过程,以便在适当的位置进行拦截.

Solving this problem will require a thorough understanding of the journey of a keystroke through the operating system, so as to intercept at the appropriate point.

我认为我需要在一个低层吃掉该事件,它仍然是一个虚拟键代码,然后提供我自己的自定义映射系统,并在系统的上方注入适当的事件.

I think I need to eat the event at a low-level, where it is still a virtual key code, then provide my own customised mapping system and inject an appropriate event further up the system.

但是在哪里(以及如何)?

But where (and how)?

我在下面的答案中详细介绍了我的研究结果(也许应该在某个时候重新移植到问题中).

推荐答案

我正在制作此社区Wiki,欢迎对其进行改进.

I'm making this community wiki, please feel welcome to improve it.

我问过的子问题:

  • Make SHIFT+3 produce `#` not `£` on OSX by code
  • How to tap (hook) F7 through F12 and Power/Eject on a MacBook keyboard
  • How to tap/hook keyboard events in OSX and record which keyboard fires each event
    -> https://github.com/Daij-Djan/DDHidLib
  • Trap each SHIFT key independently on OS X
  • In OSX, how to determine which keyboard generated an NSEvent?

我可以在中间层的底部拦截几乎所有的keydown/keyup事件.除了电源,以及从ON到OFF转换时的CAPSLOCK键.

I can intercept almost all keydown/keyup events at the bottom of the middle tier. Except for power, and also CAPSLOCK key-UP when it is transitioning from ON to OFF.

非常讨厌!

在底层层次上,除了PowerKey之外,我可以将所有键调高/调低.

Working at the bottom tier level, I can get all key up/down except for the PowerKey.

如果不是CapsLock如此尴尬的75%成功率,我将有一个很好的解决方案.令人烦恼的是,在如此有用的位置处理密钥会极大地提高所需的复杂性.

If it were not for that awkward 75% success rate for CapsLock I would have a good solution. It is vexing that handling a key in such a useful location massively escalates the required complexity.

我找到了DDHidLib,目前正在仔细检查以找出它是否可以解决该问题.

I have found DDHidLib and am currently looking through it to figure out if it smoothes that problem.

搜索"keyEventWithType CGEventTapCreate"似乎是个好主意,因为这些是挖掘事件并重新发射的必不可少的要素.

Googling "keyEventWithType CGEventTapCreate" seems like a good idea, as those are essential ingredients for Tapping the event and Re-Emitting it.

是的! 修改keyDown输出-可以编译代码,并进行较小的调整(CGEventMaskBit( kCGEventKeyDown ) | CGEventMaskBit( kCGEventFlagsChanged ),),我可以选择修饰键也.我为LSHIFT和RSHIFT获得了不同的键码.太棒了!

Yay! Modify keyDown output -- that code compiles, and with minor tweaking (CGEventMaskBit( kCGEventKeyDown ) | CGEventMaskBit( kCGEventFlagsChanged ),) I can pick up modifier keys also. I get different keycodes for LSHIFT and RSHIFT. Brilliant!

上述问题:

  • 点击kCGEventKeyDown可用于某些功能键,但不适用于其他功能键.看起来苹果似乎只重载了某些键,而重载的键似乎陷入了较低的境界.
  • 电源/弹出键没有被抓住.
  • 我看不出有任何方法可以消除击键来自哪个设备.

如何在MacBook键盘上点按(挂钩)F7至F12和电源/弹出

http://blog.tklee.org/2008/09/Modifys-across-keyboards.html
-> http://forums.macrumors.com/showthread.php?t=778484
-> https://github.com/maravillas/dual-keyboards

http://blog.tklee.org/2008/09/modifiers-across-keyboards.html
-> http://forums.macrumors.com/showthread.php?t=778484
-> https://github.com/maravillas/dual-keyboards

https://github.com/pkamb/PowerKey 可能会提供一些见识
-> https://github.com/pkamb/PowerKey/blob/master /PowerKey/PKPowerKeyEventListener.m
-> Apple键盘媒体键事件处理-流氓变形虫
... Mac OS X的系统范围快捷键
-> http://snippets .aktagon.com/snippets/361-registering-global-hot-keys-withcocoa-and-objective-c

https://github.com/pkamb/PowerKey may provide some insight
-> https://github.com/pkamb/PowerKey/blob/master/PowerKey/PKPowerKeyEventListener.m
-> Apple Keyboard Media Key Event Handling -- Rogue Amoeba
... system wide shortcut for Mac OS X
-> http://snippets.aktagon.com/snippets/361-registering-global-hot-keys-with-cocoa-and-objective-c

另一个问题是:LSHIFT向下RSHIFT向下& up LSHIFT向上. RSHIFT事件不会被捕获.

Another problem is: LSHIFT-down RSHIFT-down&up LSHIFT-up. The RSHIFT events wouldn't get caught.

好像我需要深入研究IOKit

Looks like I need to dip down into IOKit

使用IOHIDManager获取修饰符键事件
-> https://github.com/candera /khordr/blob/master/src/c/keygrab/hid-scratch.c

Using IOHIDManager to Get Modifier Key Events
-> https://github.com/candera/khordr/blob/master/src/c/keygrab/hid-scratch.c

kEventRawKeyDown在:

kEventRawKeyDown in:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/
Developer/SDKs/MacOSX10.10.sdk/System/Library/
Frameworks//Carbon.framework/Frameworks/HIToolbox.framework/
Headers/CarbonEvents.h


资源

3层:

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆