Re: Keystrokes for non-ascii letters
Re: Keystrokes for non-ascii letters
- Subject: Re: Keystrokes for non-ascii letters
- From: Ricky Sharp <email@hidden>
- Date: Sat, 29 Nov 2008 11:41:02 -0600
On Nov 29, 2008, at 10:26 AM, Dave DeLong wrote:
I'm working on a client/server application. The client sends tiny
"Event" objects to the server that can contain an NSString of a
letter or short sequence of letters (up to about 4). What I'm
trying to do is figure out how the server can perform the
keypress(es) to get that letter (or sequence).
I know that I could use CGEventCreateKeyboardEvent, but that
requires me to know the precise keycode for the letter. If I were
limited to just ASCII 0-127, that wouldn't be so bad. But I could
also be getting things like "é" or "£" and so on. I'd rather not
hard code in every keystroke combination. =)
Alternatively, I could try using AppleScript. But in playing around
with it, I found that if I do: Tell app "System Events" to
keystroke "é", then all it does is type "a". Other tests have shown
that the "keystroke" command only accepts basic ASCII characters.
Of course I could use the "using command down" to get the non-ascii
letters, but again, that would require me to hard code in every
keystroke combination.
My last idea is to put it on the clipboard and paste it in by
simulating a command-v keystroke. The only problem with this is
that command-v doesn't mean paste on all keyboards.
Do any of you have any ideas on how I can type arbitrary UTF8
characters programmatically?
As part of an automated testing framework, I generate individual
Unicode keyboard events like this:
- (void)postUnicodeKeyboardEvent_II:(unichar)aUnicodeCharacter
{
unichar theCharacters[1];
theCharacters[0] = aUnicodeCharacter;
NSString* theString = [[NSString alloc]
initWithCharacters:theCharacters length:1];
int theWindowNumber = [[applicationController_II contentWindow_II]
windowNumber];
NSEvent* theKeyboardEvent =
[NSEvent keyEventWithType:NSKeyDown location:NSMakePoint (0, 0)
modifierFlags:0 timestamp:0
windowNumber:theWindowNumber context:nil
characters:theString charactersIgnoringModifiers:nil
isARepeat:NO keyCode:0];
[NSApp postEvent:theKeyboardEvent atStart:NO];
}
In my case, I never needed to set the modifier flags, but you can
easily pass in whatever you need to above. And, depending on what
modifiers you're working with, make sure to properly set the
charactersIgnoringModifiers: param as well.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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