I'm using the standard hotkey control to allow user to specify a key combination for a certain action. However, it doesn't allow using the Return (Enter) key. Is there a way to allow it? My action is sort-of chat reply, so having a hotkey like Alt+Enter makes the most sense.
The only way would be to sub-class the control, since by default it rejects keys like VK_RETURN.
Your sub-class would probably need to do two things:
Handle the WM_GETDLGCODE message and return DLGC_WANTMESSAGE when the message indicates the enter key has been pressed
Handle the WM_KEYDOWN message when return is pressed, and manually set the hotkey in the control by sending it a HKM_SETHOTKEY message.
Related
I have a CFormView Dialog with Buttons to send commands for a hardware I/O.
I the user accidentally click the enter key, the command is executed. (the last button one, that has the focus).
How do you solve this in the correct way?
The way ist to use PreTranslateMessage. CHeck for WM_KEYDOWN and VK_ENTER. Ignore it or do whatever you want.
Background: CFormView::PreTranslateMessage later calls PreTranslateInput and this finally calls IsDialogMessage and this functions translates the Enter key to execute the default dialog button.
I am using an On_Command handler, and want to specifically use the return and escape keys for different functionalities. Please help me to understand how to do it as i am kinda struck. Thanks.
This is what i have done so far ON_COMMAND(IDC_CALC, &CALC::OnGlobalCALC)
and the handler is
void CALC::OnGlobalCALC()
{
//Please suggest what to code here
};
Keyboard input is usually sent via WM_KEYDOWN (OnKeyDown) messages. So you can handle them there if you have a CView or CFrameWnd.
But this messages are only sent to the window that has the focus.
If Enter and Escape have a global functionality in an application you can assign and use an accelerator table. In this case you can define that enter or escape are handled by the accelerator (usually in PreTranslateMessage) and are converted into a WM_COMMAND message.
But what you want and need to use is heavily dependent on what you really want and what kind of windows you have.
So I'm making an MFC application that handles a couple of different messages and will display different output based on which message was handled. So right now I have one that handles the WM_KEYDOWN message and displays that message's output. Now I also have one that handles WM_RBUTTONDOWN and what I want it to do is to start up the game of Brick Breaker that I am making. The issue that I'm having is that once I enter the WM_RBUTTONDOWN I want to disable certain keys so that I can control the paddle without calling the WM_KEYDOWN.
TL:DR How do you disable certain keys from the WM_KEYDOWNin MFC.
You can override PreTranslateMessage to see and bypass a message before MFC does its message map translation.
You shouldn't have to disable keys in your application. When another program has the focus, all input should go to that program.
You will want to forward the keys you are interested in to the code that drives your object.
You don't need to disable keys. It is simply up to your code to decide to process or not a key according to the state of the application.
I added a context menu to a MFC CRichEditCtrl, it includes a delete option which does:
ReplaceSel("",TRUE);
It appears to work but when I look at the undo log, it's not the same... we end up with characters being lost at the end of the sequence.
Any ideas how I can make my code be the same as what happens when you press DELETE? Or even reuse that default functionality?
SetFocus to edit window, and then keybd_event of delete key?
Or see if a WM_COMMAND message is triggered when the del key is hit and send the same message. You could have a look at their resources (using ResHacker or the like) and see if the DEL key is an accelerator for an existing WM_COMMAND message, or just use SpyXX.
I'm trying to get my application to do something when CTRL+S is pressed. I'm just not sure how the W and L params work for WM_KEYDOWN. MSDN has something about bit fields which i'm not sure about. How can I detect CTRL and S?
Thanks
What do I do if another control aside from hWnd has focus?
Well, this is the big list of virtual key codes.
CTRL-S is going to be sent through as 2 WM_KEYDOWN messages - a message when the ctrl key is pressed (VK_LCONTROL or VK_RCONTROL) followed by a 0x53 for the "S" key.
Rather than processing both messages, wait for the key down message for the 'S' press then call GetKeyState using the magic value VK_CONTROL (otheriwse you'd need to test individually for the left AND right control keys) to see if the S was pressed with CTRL held down.
--
Obviously, keyboard messages are sent directly to the window that has focus. To get accelerator combinations to work at the application scope you need to check the messages before dispatching them to the focus window - i.e. in your message pump. See the documentation for TranslateAccelerator.
If you want to handle system wide keypresses, the other answer points to the hot key api.
When the WPARAM is equal to the CTRL VKcode, then set a bool in the state of your object. Then, when S comes up, if Ctrlbool, you've got CTRL-S.