views:

40

answers:

1

Hi,

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);

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

A: 

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

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.

Poly