Re: Sending a keystroke
Re: Sending a keystroke
- Subject: Re: Sending a keystroke
- From: Sam Taylor <email@hidden>
- Date: Sat, 21 Sep 2002 20:24:36 +0100
Hello,
See CGPostKeyboardEvent in CGRemoteOperation.h (in the
ApplicationServices framework), as shown below.
I have my own question on this subject - how do I get the computer to
(temporarily) ignore any modifier keys the user is holding. If I am
trying to send a keyboard shortcut to an application, but the user is
holding down option then either the wrong thing will happen or the
computer will just beep. Using CGPostKeyboardEvent to simulate keyups
for all modifiers down doesn't seem to work. Has anyone solved this?
Sam Taylor
/*
Synthesize keyboard events. Based on the values entered, the
appropriate key down, key up, and flags changed events are generated.
If keyChar is NUL (0), an apropriate value will be guessed at, based on
the default keymapping.
All keystrokes needed to generate a character must be entered,
including SHIFT, CONTROL, OPTION, and COMMAND keys. For example, to
produce a 'Z', the SHIFT key must be down, the 'z' key must go down,
and then the SHIFT and 'z' key must be released:
CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)56, true ); // shift
down
CGPostKeyboardEvent( (CGCharCode)'Z', (CGKeyCode)6, true ); // 'z'
down
CGPostKeyboardEvent( (CGCharCode)'Z', (CGKeyCode)6, false ); // 'z' up
CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)56, false ); // 'shift
up
*/
typedef u_int16_t CGCharCode; // Character represented by event, if
any
typedef u_int16_t CGKeyCode; // Virtual keycode for event
CG_EXTERN CGEventErr CGPostKeyboardEvent( CGCharCode keyChar,
CGKeyCode virtualKey,
boolean_t keyDown );
Stephane ODUL wrote:
>
Hi,
>
>
I have a cocoa tool (background app) that must simulate a keystroke
>
from the user. It's a very simple thing. I just want to send a Print
>
and a Close command: Cmd+P or Cmd+W, as those shortcuts are some kind
>
of system wide it should work with most of the available apps.
>
>
I've tried to use the AppleScript approach but this do not work will
>
all the applications.
>
>
Any guide on which functions I have to use, or some sample code would
>
be great.
>
>
Thank you
>
>
Stephane ODUL
>
http://www.soft.odul.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.