Thread: How can I control keybord button with c?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    How can I control keybord button with c?

    Hello,
    I have been looking for controling codes of buttons in c. For example how can I make my program do a specific action when I press b ? Or how can I make my program press b by itsself? . Any source code would be appreciated.

    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Under which circumstances? In Windows, Linux, Dos, OS/2 ... ?

    In Windows, you can certainly make an application think you pressed a key on the keyboard [buttons, I think of buttons in dialog boxes or like the Submit Reply button when I'm replying to a post, rather than keys on the keyboard]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I have meant the keys on the keybord. For example how can I make my program exit when I press q ? But I dont want to press enter after I press q. I want it to exit just after I press the q button on the keybord.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Windows or Linux (or other OS?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    windows.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I copied this from somewhere:
    Code:
    DWORD origKBMode;
    
    void SetKbdModeRaw(void)
    {
    	DWORD mode;
    	GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode);
    	if (origKBMode == 0)
    		origKBMode = mode;
    	mode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
    	SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode);
    }
    
    void RestoreKbdMode(void) 
    {
    	SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), origKBMode);
    }
    If you still want to see what you are typing, but want the application to immediately get the input, remove the "ENABLE_ECHO_INPUT" from the above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am sorry I did unterstand codes slightly , but I couldnt understand how I can use it. Sorry for my lack of experience.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you don't really need to UNDERSTAND how it works. [I don't quite know what's happening INSIDE the calls either - I have a fair idea, but that's about it]

    The code I gave will basicly set the standard input so that it's "raw", which means that the input is not processed and held until you press enter. Instead, each keypress is sent directly to the application when it's made.

    I missed out the include:
    Code:
    #include <windows.h>
    You then just call SetKbdModeRaw() at start of your program, and read the keyboard one key at a time, using for example getch().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Wow, I got what you said, but I saw that getch() funct. for the first time. And interestingly , I dont need to call SetKbdModeRaw() if I use getch(). getch() already does what I want. But why ? nOW ı am confused . =)

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ozumsafa View Post
    Wow, I got what you said, but I saw that getch() funct. for the first time. And interestingly , I dont need to call SetKbdModeRaw() if I use getch(). getch() already does what I want. But why ? nOW ı am confused . =)
    I recommend a visit to the FAQ.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    ukkeyy. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbee Q: reg. MFC dlg button & Vista UAC shield icon
    By colbyringeisen in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2008, 05:16 PM
  2. Static Control Bitmap Style
    By Nurfina in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2007, 05:51 AM
  3. Button and edit control border
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2005, 02:31 PM
  4. Updating a list control
    By MPSoutine in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2003, 02:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM