Re: virtual keycode to character
At 7:22 PM -0500 4/29/09, Ken Thomases wrote: On Apr 29, 2009, at 6:40 PM, kvic...@pobox.com wrote: thank you. this solved my problem. You're welcome. I'm glad I could help. and (i think) you've helped again. thanx. (see below) and for the archives (to help anyone in the future), here is my code: UInt32 deadKeyState = 0; UniCharCount actualCount = 0; UniChar baseChar; TISInputSourceRef sourceRef = TISCopyCurrentKeyboardLayoutInputSource(); CFDataRef keyLayoutPtr = (CFDataRef)TISGetInputSourceProperty( sourceRef, kTISPropertyUnicodeKeyLayoutData); CFRelease( sourceRef); UCKeyTranslate( (UCKeyboardLayout*)CFDataGetBytePtr(keyLayoutPtr), [theEvent keyCode], kUCKeyActionDown, 0, LMGetKbdLast(), Why LMGetKbdLast rather than LMGetKbdType? I think that the Last in LMGetKbdLast means former. That is, if the keyboard is switched at some point, then LMGetKbdType gives the current keyboard type and LMGetKbdLast gives the previous keyboard type. the documentation for LMGetKbdLast says: The last physical keyboard type used. i read that to mean the keyboard last used/typed on by the user, i.e., the keyboard used that triggered the current keyboard event. i'm assuming that you are correct and i've changed my code appropriately. thanx, ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: virtual keycode to character
ken, thank you. this solved my problem. and for the archives (to help anyone in the future), here is my code: UInt32 deadKeyState = 0; UniCharCount actualCount = 0; UniChar baseChar; TISInputSourceRef sourceRef = TISCopyCurrentKeyboardLayoutInputSource(); CFDataRef keyLayoutPtr = (CFDataRef)TISGetInputSourceProperty( sourceRef, kTISPropertyUnicodeKeyLayoutData); CFRelease( sourceRef); UCKeyTranslate( (UCKeyboardLayout*)CFDataGetBytePtr(keyLayoutPtr), [theEvent keyCode], kUCKeyActionDown, 0, LMGetKbdLast(), kUCKeyTranslateNoDeadKeysBit, deadKeyState, 1, actualCount, baseChar); ken p.s. i realize this might have a bug if the unicode for the unmodified keyboard character is more than 1 unicharacter. At 9:53 PM -0500 4/27/09, Ken Thomases wrote: On Apr 27, 2009, at 7:46 PM, kvic...@pobox.com wrote: in my app, i allow the user to specify keyboard shortcuts for menu items (in a manner similar to xcode). for the string passed to -[MenuItem setKeyEquivalent], i pass the string obtained from the current event via [NSEvent charactersIgnoringModifiers]. this works fine almost all the time. however, for some keyboard presses it doesn't work properly, namely for shift and some of the number keys, eg., command-! (command exclamation mark) doesn't work as a keyboard equivalent, but shift-command-1 (shift command one) does work. thus, i would like a reliable way to go from virtual keycode to the non-shifted character. in searching the archives, this topic has come up several times, but all proposed solutions that i've found make use of one or more deprecated (in leopard) api calls. eg., LMGetKBDType or GetScriptManagerVariable, etc. does anyone have a solution that doesn't rely on any deprecated api calls? You can use TISCopyCurrentKeyboardLayoutInputSource to get the current keyboard input source. Then, you can query it using TISGetInputSourceProperty with kTISPropertyUnicodeKeyLayoutData to get the 'uchr' data for that keyboard layout. With that, you can call UCKeyTranslate. By the way, LMGetKbdType is not deprecated, at least according to its declaration in the headers. It is documented on a page which is, as a whole, marked as a legacy document. However, I don't know of a way to obtain the keyboard type otherwise (except in the context of a Carbon event). Cheers, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: virtual keycode to character
On Apr 29, 2009, at 6:40 PM, kvic...@pobox.com wrote: thank you. this solved my problem. You're welcome. I'm glad I could help. and for the archives (to help anyone in the future), here is my code: UInt32 deadKeyState = 0; UniCharCount actualCount = 0; UniChar baseChar; TISInputSourceRef sourceRef = TISCopyCurrentKeyboardLayoutInputSource(); CFDataRef keyLayoutPtr = (CFDataRef)TISGetInputSourceProperty( sourceRef, kTISPropertyUnicodeKeyLayoutData); CFRelease( sourceRef); UCKeyTranslate( (UCKeyboardLayout*)CFDataGetBytePtr(keyLayoutPtr), [theEvent keyCode], kUCKeyActionDown, 0, LMGetKbdLast(), Why LMGetKbdLast rather than LMGetKbdType? I think that the Last in LMGetKbdLast means former. That is, if the keyboard is switched at some point, then LMGetKbdType gives the current keyboard type and LMGetKbdLast gives the previous keyboard type. kUCKeyTranslateNoDeadKeysBit, deadKeyState, 1, actualCount, baseChar); Cheers, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
virtual keycode to character
in my app, i allow the user to specify keyboard shortcuts for menu items (in a manner similar to xcode). for the string passed to -[MenuItem setKeyEquivalent], i pass the string obtained from the current event via [NSEvent charactersIgnoringModifiers]. this works fine almost all the time. however, for some keyboard presses it doesn't work properly, namely for shift and some of the number keys, eg., command-! (command exclamation mark) doesn't work as a keyboard equivalent, but shift-command-1 (shift command one) does work. thus, i would like a reliable way to go from virtual keycode to the non-shifted character. in searching the archives, this topic has come up several times, but all proposed solutions that i've found make use of one or more deprecated (in leopard) api calls. eg., LMGetKBDType or GetScriptManagerVariable, etc. does anyone have a solution that doesn't rely on any deprecated api calls? short of a better solution, i may just look at the keycode obtained from the event and check to see if it corresponds to one of the constants for number keys from Events.h. thanx, ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: virtual keycode to character
On Apr 27, 2009, at 7:46 PM, kvic...@pobox.com wrote: in my app, i allow the user to specify keyboard shortcuts for menu items (in a manner similar to xcode). for the string passed to - [MenuItem setKeyEquivalent], i pass the string obtained from the current event via [NSEvent charactersIgnoringModifiers]. this works fine almost all the time. however, for some keyboard presses it doesn't work properly, namely for shift and some of the number keys, eg., command-! (command exclamation mark) doesn't work as a keyboard equivalent, but shift-command-1 (shift command one) does work. thus, i would like a reliable way to go from virtual keycode to the non- shifted character. in searching the archives, this topic has come up several times, but all proposed solutions that i've found make use of one or more deprecated (in leopard) api calls. eg., LMGetKBDType or GetScriptManagerVariable, etc. does anyone have a solution that doesn't rely on any deprecated api calls? You can use TISCopyCurrentKeyboardLayoutInputSource to get the current keyboard input source. Then, you can query it using TISGetInputSourceProperty with kTISPropertyUnicodeKeyLayoutData to get the 'uchr' data for that keyboard layout. With that, you can call UCKeyTranslate. By the way, LMGetKbdType is not deprecated, at least according to its declaration in the headers. It is documented on a page which is, as a whole, marked as a legacy document. However, I don't know of a way to obtain the keyboard type otherwise (except in the context of a Carbon event). Cheers, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com