Question

I'm trying to figure out how to go about making an app that will have buttons that will when pressed cause my mac to key a certain character (or modifier). I've already got the buttons laid out exactly as a want them so my project is now just two steps away from being complete:

  1. send a message to the mac to let it know which key to press.
  2. have the mac press the actual key For 1 Im thinking of setting up a bonjour service which which send a string associated with the key that is pressed on the ipad to the mac. the mac would then receive this. Some of the keys on my keyboard a characters that require shift (i.e. @) so im thinking for this the code would be something like S120. Whatever receives the message would then read the string see that it needs to stimulate shift the press the key associated with 120. For 2 I have less idea. Keep in mind this needs to be fast as it will be keyboard. Im thinking of making an iokit project that will receive the string decode it and simulate the correct key. This could be overkill though, and I've heard that you really have to be careful when dealing with the kernel. If my ideas are incorrect, please suggest a better way and fill in details. Thanks!
Était-ce utile?

La solution

As for simulating the keypress, there's an easy way to do this using Applescript.

tell application "System Events"
    keystroke "A"
end tell

(You might need to "enable access for assistive devices" in the Accessibility prefpane).

Alternatively, the same script can be run on the command line

osascript -e 'tell app "System Events" to keystroke "a"' 

EDIT: If you're worried about speed, the scripting bridge can help.

#import <Foundation/Foundation.h>
#import <ScriptingBridge/ScriptingBridge.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {  
        id sysEvents = [SBApplication applicationWithBundleIdentifier: @"com.apple.systemevents"];

        [sysEvents keystroke: @"h" using: 'Ksft'];
        [sysEvents keystroke: @"e" using: 0];
        [sysEvents keystroke: @"l" using: 0];
        [sysEvents keystroke: @"l" using: 0];
        [sysEvents keystroke: @"o" using: 0];

        // keyCode might be more suitable for your purposes

        for (int i = 32; i < 64; i++) 
        {
            [sysEvents keyCode: i using: 0];    
        }
    }
}

You can find key codes using this app.

http://manytricks.com/keycodes/

Autres conseils

You probably want the CGEventCreateKeyboardEvent() function or similar in the Quartz event services.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top