views:

358

answers:

1

I'm working on a pre-existing codebase and I'm looking to have the user type any 1-2 digits followed by the enter key at any time during the code being run and pass that number to a function. Currently, user input is handled like so:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;

    switch (message)
    {
    case WM_KEYDOWN:
        Engine::GetInstance()->GetInput()->GetKeyboard()->SetKeyPressed(static_cast<int>(wParam));
        break;
        //snip

Now, I'm not sure of a few things,

  • a) Why would the keypressed be passed as an integer rather than a character?

  • b) What would be the result of "F1" being sent in this case aaand

  • c) How can I use this to read in a 1-2 digit number and pass that only when enter is pressed?

+4  A: 

a) The value sent here is a virtual-key code, not necessarily a character.

b) See list of virtual key codes here (given in a comment). F1 would be represented by VK_F1 (0x70).

c) When a digit is pressed, add it to a string containing the last digit presses. When any other key is pressed, clear the string. When enter is pressed, act based on the string value.

Edit: This would be a bit complicated in WM_KEYDOWN since you would need to handle both the normal digit keys and the numpad keys. It will be easier to handle the WM_CHAR message instead, which receives the character code in wParam.

interjay
So am I going to need to do a switch for each virtual key representing 0-9? It seems like it should be able to just take in direct input, no?
Chris
No need for a switch, you can use something like: `if (key>='0' ` For digits, the virtual key codes are the same as ASCII codes
interjay
Actually, my last comment won't handle the numpad keys properly - see my edited answer.
interjay