KeyCode to Character mapping
KeyCode to Character mapping
- Subject: KeyCode to Character mapping
- From: Akhil Jindal <email@hidden>
- Date: Tue, 24 Jan 2012 13:00:53 +0530
Hi
I am developing a desktop application on cocoa and am trying to handle the
menu keyboard shortcuts on my own.
For this, I have to map the scan code that I receive into a character.
I was trying to do this using [NSEvent charactersIgnoringModifiers]. But
despite its promising name, it doesn't seem to be ignoring Shift and gives
me for eg: @ on pressing Shift+2. I wanted a '2' as the character.
On searching the net, I found a workaround of using the following code:
NSString* str = nil;
TISInputSourceRef inputSourceRef =
TISCopyCurrentKeyboardLayoutInputSource();
if (inputSourceRef)
{
CFDataRef dataRef = (CFDataRef)TISGetInputSourceProperty(inputSourceRef,
kTISPropertyUnicodeKeyLayoutData);
if (dataRef)
{
const UCKeyboardLayout* keyboardLayout = (const
UCKeyboardLayout*)CFDataGetBytePtr(dataRef);
if (keyboardLayout)
{
const UniCharCount maxStrLen = 255;
UniChar strBuff[maxStrLen];
UniCharCount actualLength = 0;
UInt32 deadKeyState = 0;
OSStatus status = UCKeyTranslate(keyboardLayout, inScanCode,
kUCKeyActionDown, inModifiers, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &deadKeyState, maxStrLen, &actualLength,
strBuff);
if (status == noErr && actualLength > 0)
{
str = [NSString stringWithCharacters:strBuff length:actualLength];
}
}
}
CFRelease(inputSourceRef);
}
This works fine for English language but when I use an IME, say for example
Cangjie, Zhuyin,etc things start to mess up. I have for eg a keyboard
shortcut of Cmd+N for opening a new file. But on pressing 'N' on these
IME's I get a japanese/chinese character which obviously doesn't match up
with 'N' and hence the shortcut doesn't work.
What I want is that even on changing to different IME's my keyboard
shortcuts should work just like US IME. On pressing 'N' I want to be able
to translate the inScanCode to 'N' despite the current IME.
For this I tried translating using TISCopy**********************************
********InputSource*F**o**r**L**a**n**g**u**a**g**e*(*(**C**F**S**t**r**i**n
**g**R**e**f**)**@**"**e**n**"*) instead
of TISCopyCurrentKeyboardLayoutInputSource(). Now, I am unable to support
different Keyboard layouts. If someone plugs-in a non standard qwerty
keyboard, the shortcuts again mess up.
So basically, what I want is that I should be able to get the character
according to the keyboard plugged-in, irrespective of the IME. I've tried a
lot of ways but am somehow not able to get it right.
Anyone knows a way I could do this?
Or is my understanding of how keyboard shortcuts are supposed to work with
different IME's and keyboard layouts a bit flawed? Views on that are
welcome too.
Regards,
Akhil Jindal
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden