Trap each SHIFT key independently on OS X

 ̄綄美尐妖づ 提交于 2019-12-12 04:24:20

问题


I have working code (here) that traps keycodes for non-modifier keydown events, and modifier-changed events.

But if you do:

LSHIFT down -> RSHIFT down -> RSHIFT up -> LSHIFT up

... the inner 2 actions will not trigger either of these hooks, because the modifier state is not changing!

(EDIT: Woops! I should have tested that out before writing, because actually a new modifier-changed is produced by each actual change.)

My only thought is maybe to additionally watch at an even lower level (here) -- but that doesn't look pretty no matter which angle I look at it from.


回答1:


Taken from Justin Boo's answer here

I've added some more modifier just in case someone stumbles upon this and wants other keys as well.

- (void) flagsChanged:(NSEvent*)theEvent{
    if ([theEvent modifier] == 131330){
        //do stuff regarding left shift
    }else if ([theEvent modifier] == 131332){
        //do stuff regarding right shift
    }else if ([theEvent modifier] == 65792){
        //caps lock is on
    }else if ([theEvent modifier] == 8388864){
        //FN key pressed
    }else if ([theEvent modifier] == 262401){
        //control key pressed
    }else if ([theEvent modifier] == 524576){
        //option key pressed
    }else if ([theEvent modifier] == 1048840){
        //command key pressed
    }else if ([theEvent modifier] == 256){
        //there are no modified pressed and caps lock is off
    }
}

I recommend storing some BOOLs in your class such as LShiftDown and RShiftDown as this method should be called when the modifiers are pressed. You can probably also detect this property in your keyDown implementation to detect differences such as "a" and "A".



来源:https://stackoverflow.com/questions/30394032/trap-each-shift-key-independently-on-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!