Thread: keybd_event() problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    12

    keybd_event() problem

    I am trying to simply get an 'I' printed onto the screen and that is it, but I can't seem to get it done. Does anybody see a problem in the code? Thanks in advance for your help.


    EDIT:: Right now it is printing a '1' not a letter which I am aiming to do. Any help? Thanks.

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    void AltEnter(void);
    
    int main()
    {
        Sleep(2000);
           
        AltEnter();
        
        Sleep(5000);
        cout << "\nEnter 1 to end: ";
        int number = 0;
        while (number != 1) {
              cin >> number;
              }
       
        return 0;
    }
    
    void AltEnter()
    {
        keybd_event(49,
            0,
            0,
            0);
        keybd_event(49,
            0,
            KEYEVENTF_KEYUP,
            0);
        return;
    }
    EDIT:: Right now it is printing a '1' not a letter which I am aiming to do. Any help? Thanks.
    Last edited by scrub05; 01-10-2005 at 04:15 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Use CODE tags, not quote tags, so your indentation stays. Why can't you just use cout? And could you expand a bit more on how exactly this isn't working for you?

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    void AltEnter()
    {
        keybd_event('I',
            0,
            0,
            0);
        keybd_event('I',
            0,
            KEYEVENTF_KEYUP,
            0);
        return;
    }
    needs to be:
    Code:
    void AltEnter()
    {
        keybd_event(49,
            0,
            0,
            0);
        keybd_event(49,
            0,
            KEYEVENTF_KEYUP,
            0);
        return;
    }
    That snippet works, but probably not how you want it to. I usually get a "Press any key to continue" message after my console program runs, but since I'd already sent a keystroke, it closed immediately. To print to the screen, use cout.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    12
    You are right, a little background will be helpful. I'm starting small to get the hang of keybd_event(), to eventually have a program that sends keystrokes to another window. BUT, right now with this small snipplet that I have posted I just want it to put a letter/number on the screen or move the mouse. Thanks for all of your collective help so far.

  5. #5
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Sorry for stealing the thread, but I just wonder, in functions declared void, is having return; necessary?

  6. #6
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    No, it is not necessary.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Sorry for stealing the thread, but I just wonder, in functions declared void, is having return; necessary?
    Though it's convenient if you need to end the function in a conditional or something like that.

  8. #8
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136

    Though it's convenient if you need to end the function in a conditional or something like that.
    Yep. I generally don't use return; at all unless I return; in a conditional. In that case I also put a return; at the end. I don't need it, but... I like it (for consistency).

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    @scrub05: I really can't help you any further. I don't know enough about Windows programming to give you any advice. Try this snippet:
    Code:
    void AltEnter(void);
    
    int main()
    {
    	char p;
    	AltEnter();
    	cin >> p;
    	cout << endl << "You entered " << p << endl;
    	return 0;
    }
    
    void AltEnter()
    {
    	keybd_event(0x49,
    	0,
    	0,
    	0);
    
    	keybd_event(0x49,
    	0,
    	KEYEVENTF_KEYUP,
    	0);
    	
    	keybd_event(VK_RETURN,
    	0,
    	0,
    	0);
    
    	keybd_event(VK_RETURN,
    	0,
    	KEYEVENTF_KEYUP,
    	0);
    
    	return;
    }
    @hijackers: I usually put the return in, even if it's not needed. I have a bad habit of not planning far enough ahead and needing to change a void function to a function that actually returns a value. Then it's pretty handy to just find the return statements and tack the return value on the end.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM