-charactersIgnoringModifiers and the shift key
-charactersIgnoringModifiers and the shift key
- Subject: -charactersIgnoringModifiers and the shift key
- From: John Stiles <email@hidden>
- Date: Thu, 17 Apr 2008 09:02:59 -0700
I have an NSEvent and I need to know what key the user has pressed,
minus any of the modifiers. NSEvent -charactersIgnoringModifiers seems
like a good place to start, but it has one serious flaw—it does not
ignore the Shift key. So, for instance, it won't change ~ to `, ! to 1
or { to [.
I need this for my app, because I am trying to implement functionality
where the user can add custom hotkeys to buttons or menu items—this is a
requirement of the design, it works well conceptually, and it works well
on the Windows side. On the Mac side, however, there are lots of
problems where AppKit will get confused when two items have similar
hotkeys (such as rdar://5848023 [NSResponder] Problem with button
hotkeys and shift modifier, which I posted about here before) and the
end result is that it, with some key combinations, AppKit sends the
action message to the wrong menu item or button.
To work around these issues, when the user presses a hotkey, I am
looking at [NSApp currentEvent] to see what key is actually pressed, and
given that info I can easily figure out what button or menu item needs
to be invoked on my own. To do this, I use [event
charactersIgnoringModifiers] and [event modifierFlags] and then checking
a dictionary of all the hotkeys. For most things, this works well. Where
it falls down is shifted punctuation, because my dictionary has the
hotkey listed as "cmd+shift+[", but the event is telling me that the
user pressed "cmd+shift+{". Of course, "cmd+shift+{" isn't in my table
at all, so it doesn't work.
I could bite the bullet and hard-code the unshifted versions of all the
keys on the US keyboard, and that would make Americans happy, but I
expect we will eventually need to "do the right thing" so I need to
actually get the unshifted character out of this NSEvent. So how can
this be done? Any pointers would be helpful.
_______________________________________________
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