How to go about making app for ipad that makes it into a Mac keyboard [closed]

给你一囗甜甜゛ 提交于 2019-12-01 12:02:20

问题


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!

回答1:


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/




回答2:


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



来源:https://stackoverflow.com/questions/15041919/how-to-go-about-making-app-for-ipad-that-makes-it-into-a-mac-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!