• 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: email@hidden
  • Date: Sat, 21 Sep 2002 19:43:12 +0200

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.

  • Follow-Ups:
    • Re: Sending a keystroke
      • From: Stephane ODUL <email@hidden>
    • Re: Sending a keystroke
      • From: Stephane ODUL <email@hidden>
References: 
 >Sending a keystroke (From: Stephane ODUL <email@hidden>)

  • Prev by Date: NSTextField Focus
  • Next by Date: Re: Scaling down an image proportionally
  • Previous by thread: Sending a keystroke
  • Next by thread: Re: Sending a keystroke
  • Index(es):
    • Date
    • Thread