在Mac OSX中模拟ioquake3的按键事件 [英] Simulate key press events for ioquake3 in Mac OSX

查看:93
本文介绍了在Mac OSX中模拟ioquake3的按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个应用程序,该应用程序在Mac OSX中模拟按键事件以控制ioquake3.我当前的方法是使用Quartz Event Services创建按键事件.这适用于例如TextEdit,但不支持ioquake3.

I am currently writing an application which simulates key press events in Mac OSX to control ioquake3. My current approach is to create the key press events with the Quartz Event Services. This works fine with e.g. TextEdit but not with ioquake3.

CGKeyCode keyCode = 126; // 126 is the "up" key
CGEventRef event1 = CGEventCreateKeyboardEvent (NULL, keyCode, true);
CGEventPost(kCGSessionEventTap, event1);
// CGEventPost(kCGHIDEventTap, event1);
// CGEventPost(kCGAnnotatedSessionEventTap, event1);
CFRelease(event1);

CGEventRef event2 = CGEventCreateKeyboardEvent (NULL, keyCode, false);
CGEventPost(kCGSessionEventTap, event2);
// CGEventPost(kCGHIDEventTap, event2);
// CGEventPost(kCGAnnotatedSessionEventTap, event2);
CFRelease(event2);

我还尝试使用以下代码模拟此

I also tried to use the following code to simulate the key presses as mentioned in this post:

CGKeyCode keyCode = 126; // 126 is the "up" key
AXUIElementRef axSystemWideElement = AXUIElementCreateSystemWide();
AXUIElementPostKeyboardEvent(axSystemWideElement, 0, keyCode, true);
AXUIElementPostKeyboardEvent(axSystemWideElement, 0, keyCode, false);

还有另一种方法可以在ioquake3识别的Mac OSX中创建按键事件吗?

Is there another way to create key press events in Mac OSX which are recognized by ioquake3?

推荐答案

我解决了我的问题,并且每个有兴趣的人都在这里,以下是解决方法:

I solved my problem and everyone who is interested, here is the solution:

我将描述一个简单的例子,使事情变得清晰.如前所述,我使用Quartz Event Services来生成按键事件.如果要不断向前移动ioquake3中的角色,则必须不断产生关键的按下"事件,像这样.

I will describe a simple example which makes things clear. As mentioned before i use the Quartz Event Services, to generate the key press event. If you want to constantly move the character in ioquake3 forward, you have to constantly produce key "down" event, like this.

CGKeyCode keyCode = 126;
CGEventRef eventRef = CGEventCreateKeyboardEvent (NULL, keyCode, true);
CGEventPost(kCGSessionEventTap, eventRef);
CFRelease(eventRef);

如果要停止字符,则必须生成一个关键的向上"事件:

If you want to stop the character you have to generate one key "up" event:

CGKeyCode keyCode = 126;
CGEventRef eventRef = CGEventCreateKeyboardEvent (NULL, keyCode, false);
CGEventPost(kCGSessionEventTap, eventRef);
CFRelease(eventRef);

显然我不了解如何正确使用按键事件.

Apparently i did not understand how to use key press events in the right way.

这篇关于在Mac OSX中模拟ioquake3的按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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