Hi,

Can you tell me how can I make my application recognizes when the user presses a button on keyboard?
For example: presses 'S', application prints "Hello.".

I need this information for BorlandC++, but it would be useful even though you provide the same for VC++ :)
(I am not talking about console applications, I need these tips for GUI apps )

Thank you!

Maybe you can supply some addtional information. Why are you trying to do this? Are you perhaps thinking of making a game?

Maybe you can supply some addtional information. Why are you trying to do this? Are you perhaps thinking of making a game?

I have the game already.. not a regular game, but a Lotto Game, so I am updating it, and I need a shortcut for skipping the generating numbers part ( it can be boring ). I could add a button or something else to provide "Skip" option, but I wish to find more elegant way ( for example pressing ENTER would skip that part ).

Thank you for replying!

Are you making a window application or it's just a console? If it's a console you can use the C function getch() from the <conio.h> library. It gets the input character before it's shown.

Well.. u can just use the function getche(). It performs the same operation as getch(), but the only difference is that in getch() u have to press the enter key after u give the character, whereas in getche() u dont have to press an additional enter everytime after pressing the button.

Example:
With Getch()
(If i want to press the character 'a')
1) Press character 'a'
2) Press 'Enter'

With Getche()
1) Press character 'a'

(I am not talking about console applications, I need these tips for GUI apps )

I have mentioned I need it for GUI apps. Do you have any idea in this case?
Thank you all for effort.

//For .Net:

enum {F9_KEYID = 1, F10_KEYID = 2} ;
RegisterHotKey(0, F9_KEYID, MOD_NOREPEAT, VK_F9);
RegisterHotKey(0, F10_KEYID, MOD_NOREPEAT, VK_F10);


MSG msg = {0};
PeekMessage(&msg, 0, 0, 0, 0x0001);   //0x0001 means No Repeat..
TranslateMessage(&msg);
switch(msg.message)
{
    case WM_HOTKEY:
        if(msg.wParam == F9_KEYID)
        {
            MessageBox::Show("F9 Pressed");
        }
        else if(msg.wParam == F10_KEYID)
        {
            MessageBox::Show("F10 Pressed");
        }
}


For WinAPI:
while(GetMessage(&msg, 0, 0, 0))
{
    if(msg.message == WM_HOTKEY)
    {
        switch(HIWORD(msg.lParam))
        {
            case F9_KEYID : MessageBox.Show(NULL, "Key Pressed", "F9 Pressed", MB_OK); break;    //I may have mucked up my parameters here but excuse me. I'm a bit tired. I think I need to switch caption with the message. One is Caption, Other is text. Don't remember.
            case F10_KEYID : MessageBox.Show(NULL, "Key Pressed", "F10 Pressed", MB_OK); break;
            default: MessageBox.Show("some other hot key was pressed");
        }
    }

    //Translate.. Dispatch.. whatever else..
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.