(MFC, Visual C++) Keypress Detection in a dialog...

This is a discussion on (MFC, Visual C++) Keypress Detection in a dialog... within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I've got a dialog up and running that is going to detect compound keypress (keys like CTRL+5 or just ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    (MFC, Visual C++) Keypress Detection in a dialog...

    Hey,

    I've got a dialog up and running that is going to detect compound keypress (keys like CTRL+5 or just 5). I've currently got a cheap way of doing it using GetAsyncKeyState(vkey);. Whenever I am running that function in a while loop, the application freezes up until (one, or two) keys are pressed

    How can I detect keypress (the ALT,CTRL, or shift key might be pressed, depending on weather the user wants to use those keys as hotkeys). Also, it needs to detect mouse clicks, like mouse buttons 1-5. (left mouse, middle, right, xbutton1, xbutton2).

    I also tried overiding the PreTranslateMessage to detect for WM_KEYDOWN and WM_KEYUP messages, but I don't like this way at all, very "code-hackish".

    I'm just trying to provide an interface where the user clicks a button to "set the xxxxx hotkey" and then a dialog pops up with keys the user may be pressing (ALT+5 or CTRL+T or Z or 4 (or any other 1-2 key combonation ).

    I've done a lot of Googling on this topic, and my searches haven't resulted in much success :|

    Any help given will be greatly appreciated.

    Thanks in advance,

    Guitarist809
    Last edited by guitarist809; 08-30-2008 at 10:30 PM.
    ~guitarist809~

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    It sounds as if you are doing the key detecting on the main thread.
    Spawn a different thread (AfxBeginThread) that detects presses instead and let the main loop do its message pumping.
    Also consider using a good API for this kind of thing, such as DirectInput.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Elysia View Post
    It sounds as if you are doing the key detecting on the main thread.
    Spawn a different thread (AfxBeginThread) that detects presses instead and let the main loop do its message pumping.
    Also consider using a good API for this kind of thing, such as DirectInput.
    Thanks for the response.

    Sounds like a good idea. However, there are a few problems with that. When the user is pressing keys, a textbox is filled with information (what key the user pressed). Should I just put the HWND in a struct (along with other information), pass that to the thread, and then when a user presses a key just do a SendMessage() with WM_SETTEXT to the control's HWND?

    Also, I thought DirectInput was depreciated (or are there two different DirectInputs, where one is DX and the other isn't).

    Lastly, I have no clue how to detect DirectInput . Google isn't giving much help on this topic. Could *maybe* someone give an example on how to detect DirectInput keys?


    Thanks,

    Guitarist809
    ~guitarist809~

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I have no idea whether it's deprecated or not, but MSDN says to start here. (Anything Microsoft, you should head to MSDN.)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by guitarist809 View Post
    Sounds like a good idea. However, there are a few problems with that. When the user is pressing keys, a textbox is filled with information (what key the user pressed). Should I just put the HWND in a struct (along with other information), pass that to the thread, and then when a user presses a key just do a SendMessage() with WM_SETTEXT to the control's HWND?
    Actually, the best idea is to have some functor system (for C++), so the input thread can notify the GUI thread to update the GUI. Updating the GUI on a thread that didn't create the window is a bad idea usually.

    Lastly, I have no clue how to detect DirectInput . Google isn't giving much help on this topic. Could *maybe* someone give an example on how to detect DirectInput keys?
    It's been so long since I last did it, so I can't give much good examples.
    However, what I do know is that the SDK has some very good examples and information. If you download the DirectX SDK, you should get plenty of examples and help.
    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Context Menu & Dialog Box :: MFC
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 08-11-2002, 11:01 AM
  4. Dialog Boxes in Visual C++
    By EmilyH in forum Windows Programming
    Replies: 1
    Last Post: 09-22-2001, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21