• 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: Sending a keystroke
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sending a keystroke


  • Subject: Re: Sending a keystroke
  • From: Stephane ODUL <email@hidden>
  • Date: Sat, 21 Sep 2002 17:52:12 -0700

Works great, I just had to find the mapping for the key as it uses ADB keyboard values.

From the AppleUSBKeyboard:
static char adbkeycodes2ascii[] = "asdfhgzxcv_bqwer" //00
"yt123465=97-80]o" //10
"u[ip\nlj'k;_,/nm." //20
"\t_"; //30

The command key value is 0x37. I hope this can help others.

Merci

On Saturday, Sep 21, 2002, at 10:43 US/Pacific, email@hidden wrote:

On samedi, septembre 21, 2002, at 02:15 PM, 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

From CGRemoteOperation.h in the ApplicationServices.framework:

/*
* 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 );
_______________________________________________
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.
_______________________________________________
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.

References: 
 >Re: Sending a keystroke (From: email@hidden)

  • Prev by Date: non-trivial Cocoa applescript calls?
  • Next by Date: [Newbie] Ampersands in NSURL
  • Previous by thread: Re: Sending a keystroke
  • Next by thread: Re: Sending a keystroke
  • Index(es):
    • Date
    • Thread