WM_INPUT Failing to Detect Arrow Keys
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: WM_INPUT Failing to Detect Arrow Keys

  1. #1
    Join Date
    Apr 2011
    Posts
    21

    WM_INPUT Failing to Detect Arrow Keys

    Hi guys,

    I'm not getting the response I expect from WM_INPUT detecting the arrow keys:

    Code:
    #define key_right 0x27
    
    USHORT key=raw->data.keyboard.VKey;
     
    if(key == key_right){
     if(raw->data.keyboard.Flags == 0)
      face_right=1;  // breakpoint one : never triggers (on keydown or keyup)
    else
      face_right=0; // breakpoint two : always triggers on keydown but should only trigger on keyup
    }
    I hope you can help. Thank you.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,866

    Re: WM_INPUT Failing to Detect Arrow Keys

    and raw is defined as what and is given a value from where? It would be more helpful if you posted a complete short program that demonstrated the issue.

    What is the value of data.keyboard.Flags?
    Last edited by 2kaud; April 16th, 2014 at 02:02 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,006

    Re: WM_INPUT Failing to Detect Arrow Keys

    Quote Originally Posted by endemoniada View Post
    Hi guys,

    I'm not getting the response I expect from WM_INPUT detecting the arrow keys:
    Did you try to handle WM_KEYDOWN instead?
    Besides, from MSDN
    Flags
    Type: USHORT
    Flags for scan code information. It can be one or more of the following.
    Value ...............................Meaning
    RI_KEY_BREAK (1)............The key is up.
    RI_KEY_E0 (2)..................This is the left version of the key.
    RI_KEY_E1 (4)..................This is the right version of the key.
    RI_KEY_MAKE (0).............The key is down.
    Victor Nijegorodov

  4. #4
    Join Date
    Apr 2011
    Posts
    21

    Re: WM_INPUT Failing to Detect Arrow Keys

    Thanks for trying to help, here is a mini-program:

    Code:
    void OnInput(LPARAM lParam)
    {
     UINT dwSize;
    
     GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER));
    
     LPBYTE lpb=new BYTE[dwSize];
    
     if(lpb == NULL)
      return;
    
     if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb,&dwSize,sizeof(RAWINPUTHEADER)) != dwSize){
      delete [] lpb;
      return;
     }
    
     RAWINPUT* raw=(RAWINPUT*)lpb;
    
     if(raw->header.dwType == RIM_TYPEKEYBOARD){
    
      USHORT key=raw->data.keyboard.VKey;
    
      if(key == key_right){
       if(raw->data.keyboard.Flags == RI_KEY_MAKE)
        face_right=1;
       else if(raw->data.keyboard.Flags == RI_KEY_BREAK)
        face_right=0;
      }
    
     }
    
    }
    I am getting raw->data.keyboard.Flags=2 for the arrow keys.

    It's for a game and I was hoping to keep all input (keyboard, mouse, gamepad) in a centralized location, that's why I'm not using WM_KEYDOWN.

    Thanks again.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,006

    Re: WM_INPUT Failing to Detect Arrow Keys

    Quote Originally Posted by endemoniada View Post
    I am getting raw->data.keyboard.Flags=2 for the arrow keys.
    In that case you seem to get the combination (RI_KEY_MAKE | RI_KEY_E0 ) i.e.
    (The key is down) | (This is the left version of the key)
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,866

    Re: WM_INPUT Failing to Detect Arrow Keys

    The problem is that RI_KEY_MAKE is defined to be 0 so could be taken to be this whatever the value of Flags. One way to test for up/down is
    Code:
    Face_right = !(raw->data.keyboard.Flags & RI_KEY_BREAK == RI_KEY_BREAK);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  7. #7
    Join Date
    Apr 2011
    Posts
    21

    Re: WM_INPUT Failing to Detect Arrow Keys

    It's working like a charm now. You guys are so smart, thanks a lot.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,006

    Re: WM_INPUT Failing to Detect Arrow Keys

    Quote Originally Posted by 2kaud View Post
    The problem is that RI_KEY_MAKE is defined to be 0 so could be taken to be this whatever the value of Flags.
    I don't see any problem here. Logically there can be only two possibilities for any key: either up or down.
    O if it is not the up then it is down.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)