• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CGEventKeyboardGetUnicodeString + modifier key
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CGEventKeyboardGetUnicodeString + modifier key


  • Subject: Re: CGEventKeyboardGetUnicodeString + modifier key
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Mon, 16 Apr 2007 11:22:34 +0200


On Apr 15, 2007, at 5:51 AM, email@hidden wrote:

on 2007-04-15 3:18 AM, Andrew Farmer at email@hidden wrote:

On 14 Apr 07, at 23:40, email@hidden wrote:
My question is, what is the ASCII code for a option-letter if you
know the letter?  If u is ASCII decimal 117, how do I get ASCII
159, which is ü?  And so on for other characters?

There's no fixed method; the character that results from a keypress is based on a big messy lookup process through the user's current keyboard layout and input method.

My understanding is that you can do the "big messy lookup process" yourself with the UCKeyTranslate() function in Unicode Utilities. One of the required inputs is the type of your keyboard, so that you use the right keyboard resource for the lookup. You can get your keyboard type code with CGEventSourceGetKeyboardType().

UCKeyTranslate() seems to work fine for a single key, but what about a two-key operation, something like option-u u to produce ü?

-Bill


Most of the roman keyboard do not have uchr resources but with uchr you can do like that.
It it not garantee to return a single unichar character, but it works most of the time.


UniChar string[3];
SInt32 type = LMGetKbdType();
UInt32 deadKeyState = 0;
UniCharCount stringLength = 0;
OSStatus err = UCKeyTranslate (layout,
keycode, kUCKeyActionDown, modifiers,
type, 0, &deadKeyState,
3, &stringLength, string);
if (noErr == err) {
if (stringLength == 0 && deadKeyState != 0) {
UCKeyTranslate (layout,
kVirtualSpaceKey , kUCKeyActionDown, 0, // => No Modifier
type, kUCKeyTranslateNoDeadKeysMask, &deadKeyState,
3, &stringLength, string);
}
if (stringLength > 0) {
return string[0];
}
}


For KCHR keyboard, have a look at KeyTranslate() function.

UInt32 state = 0;
UInt32 keyTrans = 0;
unsigned char result;
keyTrans = KeyTranslate(ctxt->layout, keycode | modifiers, &state); /* try to convert */
/* si result == 0 and deadkey state isn't 0... */
if (keyTrans == 0 && state != 0) {
/* ...try to resolve deadkey */
keyTrans = KeyTranslate(ctxt->layout, kVirtualSpaceKey, &state);
}
result = keyTrans;
if (!result)
result = (keyTrans >> 16);


Good luck.


_______________________________________________

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


  • Prev by Date: I want to change the background of NSWindow.
  • Next by Date: RE: makeKeyandOrderFront not working.
  • Previous by thread: controlling display sleep
  • Next by thread: taking control of the Window menu
  • Index(es):
    • Date
    • Thread