CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012
    Posts
    6

    [RESOLVED] Keyboard key press detection question.

    How do I detect what individual key was pressed in my "PeekMessage" loop.

    code:

    ...
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    // handle or dispatch messages
    if (msg.message == WM_QUIT){
    bQuit = TRUE;
    }else if (msg.message == WM_LBUTTONDOWN ){

    }else if (msg.message == WM_RBUTTONDOWN ){

    }else if (msg.message == WM_MBUTTONDOWN ){

    }else if (msg.message == WM_KEYDOWN){
    // detect pressed key ?
    }else if (msg.message == WM_KEYUP){
    // detect released key ?
    }else{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    ...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Keyboard key press detection question.

    You might want to read the documentation about all these
    WM_LBUTTONDOWN
    WM_RBUTTONDOWN
    WM_MBUTTONDOWN
    WM_KEYDOWN
    WM_KEYUP
    messages.
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Keyboard key press detection question.

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Jul 2012
    Posts
    6

    Re: Keyboard key press detection question.

    From the msdn documantation , WM_KEYDOWN has a parameter thet conatins a virtual key code , the problem is that I don't know how to read the paramiter to compare the contents with the key codes.

    I tried like this:
    ...
    }else if (msg.message == WM_KEYDOWN ){
    switch(msg){
    case WM_GETDLGCODE:{
    if(wParam==VK_ESCAPE){
    // code
    }
    }
    }
    ...

    but it doesn't work.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Keyboard key press detection question.

    Have a look at the MSG documentation
    Last edited by VictorN; July 15th, 2012 at 12:44 PM.
    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
  •  





Click Here to Expand Forum to Full Width

Featured