开发者

Getting user input from Win32 C++. WPARAM casted as int?

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 w开发者_如何学运维mId, 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜