Re: Key number to Unicode string
Re: Key number to Unicode string
- Subject: Re: Key number to Unicode string
- From: Dorian Johnson <email@hidden>
- Date: Fri, 8 Jun 2007 20:58:54 -0500
> Great thanks, Dorian,
> I write code like your before posting. Maybe I don't understand
> something - in U.S. and French layouts your and my code works fine.
> But in Russian layout uniChar variable initializes to U.S. key
values.
> And keyLayoutID stay unchanged (and equal to lastKeyLayoutID), event
> if I switch layout between consecutive calls of your method.
In a controlled test, my code appears to work fine for me (it changes
properly when switching input methods in International and produces
the correct characters for what was pressed). Here's what I was
testing with (excuse mangled formatting):
- (void)keyDown:(NSEvent *)ev
{
static UInt32 deadKeyState = 0, keyTranslateState = 0;
static SInt32 lastKeyLayoutID = 0;
UCKeyboardLayout *uchrData;
void *KCHRData;
SInt32 keyLayoutKind, keyLayoutID;
KeyboardLayoutRef keyLayout;
UInt32 charCode;
UniCharCount actualStringLength;
UniChar unicodeString[4];
unsigned char code1, code2;
UInt16 keyCode = [ev keyCode], kchrKeyCode, uniChar = 0;
unsigned int mods = [ev modifierFlags];
KLGetCurrentKeyboardLayout(&keyLayout);
KLGetKeyboardLayoutProperty(keyLayout, kKLKind, (const void **)
&keyLayoutKind);
KLGetKeyboardLayoutProperty(keyLayout, kKLIdentifier, (const void**)
&keyLayoutID);
if (lastKeyLayoutID != keyLayoutID)
{
NSLog(@"New key layout. Old: %d, new: %d\n", lastKeyLayoutID,
keyLayoutID);
deadKeyState = keyTranslateState = 0;
lastKeyLayoutID = keyLayoutID;
}
if (keyLayoutKind == kKLKCHRKind)
{
NSLog(@"Handling a kchr key event");
kchrKeyCode = (keyCode & 0x7f) | (mods & 0xffff00ff); //
this might have a slight logic error in it. be careful, test it first
kchrKeyCode = (YES /* NO if handling a key up */) ?
(kchrKeyCode & 0xff7f) : (kchrKeyCode | 0x80); // this too
KLGetKeyboardLayoutProperty(keyLayout, kKLKCHRData, (const
void **)&KCHRData);
charCode = KeyTranslate(KCHRData, kchrKeyCode,
&keyTranslateState);
// ignore code1, it's normally gibberish
//code1 = (charCode & 0xff0000) >> 16;
code2 = (charCode & 0xff);
if (keyTranslateState == 0 && code2 != '\0')
{
const char chrs[2] = {code2, '\0'};
uniChar = [[NSString stringWithCString:chrs]
characterAtIndex:0]; // I don't remember why this is used, there's
probably a reason
}
}
else
{
NSLog(@"Handling a uchr key event");
KLGetKeyboardLayoutProperty(keyLayout, kKLuchrData, (const void **)
&uchrData);
UCKeyTranslate(uchrData, keyCode, kUCKeyActionDown, mods,
LMGetKbdType(), 0, &deadKeyState, 4, &actualStringLength,
unicodeString);
uniChar = unicodeString[0];
}
NSLog(@"'%C' (virtual key 0x%x, unicode 0x%x) pressed", uniChar,
keyCode, uniChar);
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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