Keyboard emulator

This is a discussion on Keyboard emulator within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm working on a small automation solution. I want to emulate Keystrokes from an application including shift and alt ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Keyboard emulator

    Hello,
    I'm working on a small automation solution. I want to emulate
    Keystrokes from an application including shift and alt key.

    h = FindWindow (NULL, argv[1]);
    if (h != NULL)
    {
    hed = FindWindowEx (h,0,"edit",0);
    SendMessage (hed, WM_CHAR, kbchar, 0);
    }

    or

    h = FindWindow (NULL, argv[1]);
    if (h != NULL)
    {
    PostMessage (h, WM_CHAR, kbchar, 0);
    }

    doesn't do the trick for me. I need to send stuff like "CTRL+P" to an
    appliction. Any Ideas how to do that in C? Every help is appreciated .

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I think instead of sending WM_CHAR, try sending WM_KEYDOWN

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Add Keyboard emulator

    I tryed some other approches, but nothing seems to work on XP.
    For example I send a CTRL+P to the Media Player with and without
    Focus and also with force to Foreground and without:

    SetForegroundWindow(hwnd);

    SendMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0x50);

    and

    SendNotifyMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0x50);

    and

    PostMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0x50);

    The only result I got so far with the Media Player is, I get the app in
    the foreground and the app shows that only the control key is pressed without any other additional key. The only working command
    is WM_CHAR with PostMessage for apps. Is there any trick to make
    it work with Windows XP.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    A key press requires a key down as well as a key up. You should use the keybd_event() or SendInput() functions to simulate keyboard usage. To simulate Ctrl+P you need to:

    Key down down Ctrl
    Key down down P
    Key up P
    Key up Ctrl

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    3
    Thanks. keybd_event() works for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 03:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. What type of keyboard is this?
    By 7smurfs in forum Tech Board
    Replies: 5
    Last Post: 06-22-2005, 06:09 PM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21