+- (void) toggleModifier: (int)keycode {
+ // Toggle the stored state.
+ modifiers_state[keycode] = !modifiers_state[keycode];
+ // Send a keyup or keydown depending on the state.
+ qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
+}
+
+- (void) toggleStatefulModifier: (int)keycode {
+ // Toggle the stored state.
+ modifiers_state[keycode] = !modifiers_state[keycode];
+ // Generate keydown and keyup.
+ qemu_input_event_send_key_qcode(dcl->con, keycode, true);
+ qemu_input_event_send_key_qcode(dcl->con, keycode, false);
+}
+
+// Does the work of sending input to the monitor
+- (void) handleMonitorInput:(NSEvent *)event
+{
+ int keysym = 0;
+ int control_key = 0;
+
+ // if the control key is down
+ if ([event modifierFlags] & NSEventModifierFlagControl) {
+ control_key = 1;
+ }
+
+ /* translates Macintosh keycodes to QEMU's keysym */
+
+ int without_control_translation[] = {
+ [0 ... 0xff] = 0, // invalid key
+
+ [kVK_UpArrow] = QEMU_KEY_UP,
+ [kVK_DownArrow] = QEMU_KEY_DOWN,
+ [kVK_RightArrow] = QEMU_KEY_RIGHT,
+ [kVK_LeftArrow] = QEMU_KEY_LEFT,
+ [kVK_Home] = QEMU_KEY_HOME,
+ [kVK_End] = QEMU_KEY_END,
+ [kVK_PageUp] = QEMU_KEY_PAGEUP,
+ [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
+ [kVK_ForwardDelete] = QEMU_KEY_DELETE,
+ [kVK_Delete] = QEMU_KEY_BACKSPACE,
+ };
+
+ int with_control_translation[] = {
+ [0 ... 0xff] = 0, // invalid key
+
+ [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
+ [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
+ [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
+ [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
+ [kVK_Home] = QEMU_KEY_CTRL_HOME,
+ [kVK_End] = QEMU_KEY_CTRL_END,
+ [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
+ [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
+ };
+
+ if (control_key != 0) { /* If the control key is being used */
+ if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
+ keysym = with_control_translation[[event keyCode]];
+ }
+ } else {
+ if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
+ keysym = without_control_translation[[event keyCode]];
+ }
+ }
+
+ // if not a key that needs translating
+ if (keysym == 0) {
+ NSString *ks = [event characters];
+ if ([ks length] > 0) {
+ keysym = [ks characterAtIndex:0];
+ }
+ }
+
+ if (keysym) {
+ kbd_put_keysym(keysym);
+ }
+}
+