如何在OSX中创建虚拟键盘? [英] How to create virtual keyboard in osx?

查看:108
本文介绍了如何在OSX中创建虚拟键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在osx中​​创建一个虚拟键盘.是否有可能?我的意思是我可以制作一个程序,使其发出与真实键盘相同的信号.这种键盘的示例是屏幕键盘或键盘查看器(是否具有必要的界面btw).

I want to create a virtual keyboard in osx. Is it possible? I mean can I make a program that gives same signals than real keyboard. Example of this kind of keyboard would be onscreen keyboard, or keybord viewer (does it have necessary interface btw).

我应该从多低开始?我应该制作设备驱动程序吗?虚拟(无线)键盘?还是可可等有必要的东西?

How low should I start? Should I make a device driver e.g. virtual (wireless) keyboard? Or does cocoa etc. have the necessary stuff?

我的要求是:
-元组列表(时间,key_down/key_up,key_code)对应于键入的人
-虚拟键盘应与真实键盘并排工作(如触摸板和蓝牙鼠标)
-这应该适用于每个程序.我能找到的最困难的例子是:Terminal + vim,远程桌面,星际争霸之类的游戏

The requirements I have are:
- a list of tuples (time, key_down/key_up, key_code) corresponds to person typing
- virtual keyboard should work side by side with the real one (like touchpad and bluetooh mouse)
- this should work with every program. Hardest examples I can find are: Terminal+vim, remote desktop, games like starcraft

欢迎提供示例代码和链接.

Sample code and links are more than welcome.

编辑:主要要点是可以以编程方式访问击键.有类似的程序,但是它们是封闭源代码(例如 http://www.assistiveware.com/keystrokes.php ).我想知道制作这种程序的最佳方法是什么.

edit: The main point is to have programmatic access to keystrokes. There are similar programs but they are with closed source (e.g. http://www.assistiveware.com/keystrokes.php). I want to know what is the best way to make this kind of program.

现在,我开始了这个聚会.下面是一个复制-编辑-粘贴-尝试-其他代码,基本上包括制作虚拟键盘的所有必需部分.在这种情况下,每次我按下"a"时,虚拟键盘都会按下"z".有一个错误,那就是添加了多个'z'...

edit 2: Now I got this party started. Below is a copy-edit-paste-try-something-else code that basically includes all necessary parts to make a virtual keyboard. In this case, every time I press 'a' the virtual keyboard presses 'z'. There is a bug, that there is multiple 'z's added...

#import <ApplicationServices/ApplicationServices.h>

CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type,  CGEventRef event, void *refcon) {

    UniChar unicodeString[101];
    UniCharCount unicharCount; 
    char chars[2];
    int i,j,charsLen;

    CGEventRef zDown;
    CGEventRef zUp;
    zDown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
    zUp = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);

    //printf("%u %u\n", (uint32_t)type, (uint32_t) event);

    CGEventKeyboardGetUnicodeString(event, 100, &unicharCount, unicodeString);

    for (i=0; i < unicharCount; i++)
    {
        if (unicodeString[i] > 127) {
            chars[0] = (unicodeString[i] >> 8) & (1 << 8) - 1;
            chars[1] = unicodeString[i] & (1 << 8) - 1; 
            charsLen = 2;
        } else {
            charsLen = 1;
            chars[0] = unicodeString[i];
        }
        //for (j = 0; j < charsLen; j++) printf("%c", chars[j]);
    }

    if (chars[0] == 'a')
    {
        CGEventPost(kCGHIDEventTap, zDown);
        CGEventPost(kCGHIDEventTap, zUp);
    }

    return event; 
}

int main (int argc, const char * argv[]) {
    CFMachPortRef eventTap;  
    CFRunLoopSourceRef runLoopSource; 

    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
    CFRunLoopRun();

    return 0;
}

br,
Juha

br,
Juha

推荐答案

您可以使用

You could do this using Quartz Event Taps, which provides a:

...用于事件点击的C API,它们是 用于观察和更改 低级用户输入事件流 在Mac OS X中

...C API for event taps, which are filters used to observe and alter the stream of low-level user input events in Mac OS X

这篇关于如何在OSX中创建虚拟键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆