How can I press a keyboard key using Objective C?

scott

I want to allow my application to do things like hit the enter key or type words without actually touching the keys. Is this possible? How could I make an onscreen keyboard that lets you type into different applications?

zneak

The question about "how to create an onscreen keyboard" is very broad as there are many valid approaches to this, not one single and canonical way to do it. Therefore, you probably won't get much help about the high-level details of this task unless you can narrow down specific issues.

However, as far as keyboard events are concerned, you would use the Quartz Event Services API to fabricate keyboard events. Specifically, you are looking for CGEventCreateKeyboardEvent and CGEventPost.

// CGEvent functions
#import <ApplicationServices/ApplicationServices.h>

// kVK_* values
#import <Carbon/Carbon.h>

// press and release "abcd"
int virtualKeys[] = { kVK_ANSI_A, kVK_ANSI_B, kVK_ANSI_C, kVK_ANSI_D };

for (int i = 0; i < 4; i++)
{
    // press the letter
    CGEventRef event = CGEventCreateKeyboardEvent(NULL, virtualKeys[i], YES);
    CGEventPost(kCGHIDEventTap, event);
    CFRelease(event);

    // wait .1 seconds
    usleep(100000);

    // release the letter
    event = CGEventCreateKeyboardEvent(NULL, virtualKeys[i], NO);
    CGEventPost(kCGHIDEventTap, event);
    CFRelease(event);
}

For capitals, you need to "press" kVK_Shift before the letter (and you need to release it as well).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I press a keyboard key using Objective C?

From Dev

How do I press the Fn key using a PC keyboard on a Mac?

From Dev

How can I make keyboard shortcuts register on key release, rather than on key press?

From Dev

C# - How can I block the typing of a letter on a key press?

From Dev

C# - How can I block the typing of a letter on a key press?

From Dev

How can I use quotes and cedilla without having to press the right alt key in the international American keyboard layout?

From Dev

How can I receive a key press from keyboard in assembly (8086 emu)?

From Dev

Can I press the return key using code?

From Dev

I'm unable to activate a C# button by the press of an Keyboard key

From Dev

How can I press the Windows key with xdotool

From Dev

How can I press the Windows key with xdotool

From Dev

How can I display keyboard shortcuts as I press them?

From Dev

C++ How to get keyboard key press value (or number/code)

From Dev

How can I capture a key press (key logging) in Linux?

From Dev

How can I remap a keyboard key?

From Dev

Using Python, how can I implement running a loop in the foreground while waiting in the background for a key-press?

From Dev

How can I submit a form on Enter key press without using jquery/javascript?

From Dev

How to tell which keyboard was used to press a key?

From Dev

How to add keyboard shortcut to press a specific key?

From Dev

How can I optimize Objective-C singletons using ARC?

From Dev

How to achive key logger using key press event in C#

From Dev

How can I remap a quick successive double press of the A key to function as a single press of the B key in any mode?

From Dev

How can I capture a key press, outside of the form?

From Dev

How can I simulate a key press in a Python subprocess?

From Dev

How can I make "Press any key to continue"

From Dev

How can I change an underutilized keyboard key into a modifier key

From Dev

How do I silence onscreen Windows 8 "Touch Keyboard" key press sounds?

From Dev

How to press a key using nodejs?

From Dev

Can I define keyboard shortcuts using the Super key?

Related Related

  1. 1

    How can I press a keyboard key using Objective C?

  2. 2

    How do I press the Fn key using a PC keyboard on a Mac?

  3. 3

    How can I make keyboard shortcuts register on key release, rather than on key press?

  4. 4

    C# - How can I block the typing of a letter on a key press?

  5. 5

    C# - How can I block the typing of a letter on a key press?

  6. 6

    How can I use quotes and cedilla without having to press the right alt key in the international American keyboard layout?

  7. 7

    How can I receive a key press from keyboard in assembly (8086 emu)?

  8. 8

    Can I press the return key using code?

  9. 9

    I'm unable to activate a C# button by the press of an Keyboard key

  10. 10

    How can I press the Windows key with xdotool

  11. 11

    How can I press the Windows key with xdotool

  12. 12

    How can I display keyboard shortcuts as I press them?

  13. 13

    C++ How to get keyboard key press value (or number/code)

  14. 14

    How can I capture a key press (key logging) in Linux?

  15. 15

    How can I remap a keyboard key?

  16. 16

    Using Python, how can I implement running a loop in the foreground while waiting in the background for a key-press?

  17. 17

    How can I submit a form on Enter key press without using jquery/javascript?

  18. 18

    How to tell which keyboard was used to press a key?

  19. 19

    How to add keyboard shortcut to press a specific key?

  20. 20

    How can I optimize Objective-C singletons using ARC?

  21. 21

    How to achive key logger using key press event in C#

  22. 22

    How can I remap a quick successive double press of the A key to function as a single press of the B key in any mode?

  23. 23

    How can I capture a key press, outside of the form?

  24. 24

    How can I simulate a key press in a Python subprocess?

  25. 25

    How can I make "Press any key to continue"

  26. 26

    How can I change an underutilized keyboard key into a modifier key

  27. 27

    How do I silence onscreen Windows 8 "Touch Keyboard" key press sounds?

  28. 28

    How to press a key using nodejs?

  29. 29

    Can I define keyboard shortcuts using the Super key?

HotTag

Archive