osx - Map NSEvent keyCode to virtual key code -
nsevent keycode gives keyboard scan code, hardware specific code representing physical key. want convert scan code virtual key code, logical key based on users keyboard layout (qwerty, azerty, etc).
in windows can via mapvirtualkey. os x equivalent?
the virtual key code precisely not based on user's keyboard layout. indicates key pressed, not character key produce nor how it's labeled.
for example, kvk_ansi_a
(from carbon/hitoolbox/events.h, value 0x00
) not mean key produces 'a' character, means key in position 'a' key in ansi standard keyboard. if french keyboard layout active, key produce 'q'. if physical keyboard french keyboard, key labeled 'q', too.
so, virtual key code sort of akin scan code, idealized, standard keyboard. is, noted, hardware-independent. independent of keyboard layout.
to translate virtual key code character, can use uckeytranslate()
. need 'uchr' data current keyboard layout. can using tiscopycurrentkeyboardlayoutinputsource()
, tisgetinputsourceproperty()
ktispropertyunicodekeylayoutdata
property key.
you need keyboard type code. believe it's still supported use lmgetkbdtype()
that, though it's no longer documented except in legacy section. if don't that, can obtain cgevent
nsevent
, create and call cgeventsource
using cgeventcreatesourcefromevent()
, , use cgeventsourcegetkeyboardtype()
cgeventgetintegervaluefield()
kcgkeyboardeventkeyboardtype
keyboard type.
of course, it's easier use -[nsevent characters]
or -[nsevent charactersignoringmodifiers]
. or, if you're implementing text view, send key-down events -[nsresponder interpretkeyevents:]
(as discussed in cocoa event handling guide: handling key events) or -[nstextinputcontext handleevent:]
(as discussed in cocoa text architecture guide:text editing). either of call view appropriate action selector, movebackward:
, or -inserttext:
if keystroke (in context of recent events , input source) produce text.
Comments
Post a Comment