Le 6 sept. 08 à 14:00, Peter N Lewis a écrit :

At 20:15 -0600 5/9/08, Dave DeLong wrote:
How on earth can I post system keyboard events (without getting a beep)?

As Ken mentioned, first off make sure the key has somewhere to go.

After that, this is roughly the code I use in Keyboard Maestro (extracted bits and pieces, so it wont compile directly). My apologies for the appearance of the code in email, but perhaps it is still useful.


        verify_noerr( ::CGEnableEventStateCombining( false ) );
verify_noerr ( ::CGSetLocalEventsFilterDuringSuppressionState ( kCGEventFilterMaskPermitAllEvents, kCGEventSupressionStateSupressionInterval ) );
        verify_noerr( ::CGSetLocalEventsSuppressionInterval( 0.0 ) );

        PostModifierKeys( inModifiers, true );
        verify_noerr( CGPostKeyboardEvent( inChar, inKeyCode, true ) );
        verify_noerr( CGPostKeyboardEvent( inChar, inKeyCode, false ) );
        PostModifierKeys( inModifiers, false );

        verify_noerr( ::CGEnableEventStateCombining( true ) );


with:


void
UCGRemoteControl::PostModifierKeys( const UInt32& inModifiers, Boolean inKeyDown )
{
        if ( inModifiers&( UInt32(shiftKey) ) ) {
verify_noerr( ::CGPostKeyboardEvent( (CGCharCode)0, kCGShiftKeyCode, inKeyDown ) );
        }

        if ( inModifiers&( UInt32(controlKey) ) ) {
verify_noerr( ::CGPostKeyboardEvent( (CGCharCode)0, kCGControlKeyCode, inKeyDown ) );
        }
        if ( inModifiers&( UInt32(optionKey)) ) {
verify_noerr( ::CGPostKeyboardEvent( (CGCharCode)0, kCGOptionKeyCode, inKeyDown ) );
        }
        if ( inModifiers&( UInt32(cmdKey) ) ) {
verify_noerr( ::CGPostKeyboardEvent( (CGCharCode)0, kCGCommandKeyCode, inKeyDown ) );
        }
}

There is an extra piece of code to release the desired key if it is already down (if the user is pressing the A key already, and you try to press it again without first releasing it, it wont work).

Also, the key code for A is 0.  6 is Z.

It's true on QWERTY keyboard, but you should not rely on this.




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to