CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2006
    Posts
    329

    Simulate key strokes

    Hi all, I'm trying to simulate the CTRL+L key stroke combo with the keybd_event function. I'm aware that it's depreciated and that MSDN advises to use another function but it's the only thing that I was able to find an example for. So I tried the following...

    Code:
    void function(void){
        keybd_event(VK_CONTROL, 0x4C, 0, 0);
    }
    To do the CTRL+L but it doesn't seem to be working. How can I tell that I'm doing this right? Thanx in advance.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simulate key strokes

    Search this forum for SendInput. You'll find plenty of examples.

  3. #3
    Join Date
    Nov 2006
    Posts
    329

    Re: Simulate key strokes

    Ok so I took the advice and found some what of help: http://www.codeguru.com/forum/showthread.php?t=377393 But the honest truth is that it really doesn't help at all. For example, KEYBDINPUT and INPUT was a undeclared variable in DevC++. I know the compiler shouldn't make any difference when compiling/building but that's the error I got when trying to build that source. So I went to MSDN and looked them both up and says that I needed Winuser.h, or just include Windoows.h which is what I did initially. And still the error exists. Another thing I found out when searching these forums is that 7 out of 10 of the replies to either SendKeys(), SendInput() or keybd_event() were responses to search around when the only results are either "go look for yourself" type responses or incomplete code that isn't very helpful. Maybe someone would like to shed some light on the matter because I just need an example on how to do this.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simulate key strokes

    You'll need to understand how to build Win32 apps in Dev++. I can't help you with that since this is the Visual C++ forum (perhaps try the WinAPI forum).

    The code guru search I gave you have at least one or two actual code snippets. You'll need to read through them to find the actual one.

    The data is in the search, but I agree, it's a bit hard to find - here's a link to a sample:

    http://msdn.microsoft.com/en-us/magazine/cc163867.aspx

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simulate key strokes

    Quote Originally Posted by dellthinker View Post
    I know the compiler shouldn't make any difference when compiling/building but that's the error I got when trying to build that source. So I went to MSDN and looked them both up and says that I needed Winuser.h, or just include Windoows.h which is what I did initially. And still the error exists.
    Well you need to get it fixed, because you really can't build Windows apps without WinUser.h. Does it even exist on your system? If it does, did you open WinUser.h to see if that definition is in there?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Nov 2006
    Posts
    329

    Re: Simulate key strokes

    Quote Originally Posted by Paul McKenzie View Post
    Well you need to get it fixed, because you really can't build Windows apps without WinUser.h. Does it even exist on your system? If it does, did you open WinUser.h to see if that definition is in there?

    Regards,

    Paul McKenzie
    I have found a simple solution to keybd_event(). Yes my compiler is fine and so are the headers, it was the way I was calling the function which was giving me trouble. I feel I should post the code to lessen the head ache for the next guy who wants a simple starter for keybd_event(). The following holds down the Shift key with the VK_SHIFT flag and presses the 'D' button for a series of capital D's. The code....

    Code:
    int main(){
      HWND notepad=FindWindow(TEXT("NOTEPAD"), 0);
      while(1){
      Sleep(300);
      if(notepad){printf("Notepad detected!\n");
      keybd_event(VK_SHIFT, 0x2A, 0, 0);
      keybd_event(0x44,0x20,0,0); 
      keybd_event(0x44,0x20,KEYEVENTF_KEYUP,0);
      keybd_event(VK_SHIFT, 0x2A, KEYEVENTF_KEYUP, 0);}
      else{printf("Notepad not open!\n");}}
      return 0;
    }
    That should help anyone who's trying to get started. Virtual Key codes are found here: http://msdn.microsoft.com/en-us/library/ms645540
    Last edited by dellthinker; October 19th, 2010 at 02:09 PM.

  7. #7
    Join Date
    Nov 2007
    Posts
    35

    Re: Simulate key strokes

    Even more difficult in c++ is anything to do with a hotkey featuring the mouse. Suck as Winkey Click or something like that. For macro stuff the only language I found on Windows that actually grabs mouse hotkeys is AutoHotKey. I don't like the syntax or scoping. It will drive you nuts trying to figure out when to wrap a string in quotes and when not. But, for reacting to mouse hotkeys it's the only way to fly. If you are writing stuff for yourself or giveaway programs(iow nobody is saying all the code HAS to be written in c++) then my tactic is to have the main program written in whatever does the job well, and if it needs to detect a mouse hotkey, it launches a small AHK app to monitor for the hotkey.

    Try searching for c++ working code that detects something like Winkey Click and I bet you come up empty. You might find a snippet, but I bet when you run it, it doesn't really work.

    How do I know? AutoIt3 is yet another macro language for Windows. It's coded in c++. It can't really detect mouse hotkeys either. If there was a WinAPI call or library function that really did it, they would use it. The AHK guys coded it themselves which a bunch of WM_LBUTTONDOWN timing detection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured