Capture Windows KeyControls
HI,
I was trying to capture the keypress event for any key using the following code:
Code:
void CTest1Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
char chr1;
HCURSOR lhcur;
chr1 = char(nChar);
if(chr1 = 'A')
{
lhcur = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
SetCursor(lhcur);
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
When the 'A' key pressed, it is not capturing or changing the cursor state.
By the way how to capture the Windows function key.
Thanks in Advance.
Dan
Re: Capture Windows KeyControls
Take a look at this odd stuff of yours, it does not what's supposed to do:
Re: Capture Windows KeyControls
gotta use == for checking = for setting
Re: Capture Windows KeyControls
Yeah, Silly mistake.
But even then no key is getting captured when pressed.
Re: Capture Windows KeyControls
you probably have the OK and Cancel Buttons on the dialog, and either of those buttons have the focus. Remove the buttons and you will be able to see the change of icon... but only if you use any other icon other than IDC_ARROW, as IDC_ARROW will be the default one... Use either IDC_WAIT or IDC_CROSS to see the effect...
Cheers...
Mohan.
Re: Capture Windows KeyControls
I changed the code as
Code:
void CTest1Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
char chr1;
HCURSOR lhcur;
chr1 = char(nChar);
if(chr1 == 'A')
{
lhcur = AfxGetApp()->LoadStandardCursor(IDC_WAIT);
SetCursor(lhcur);
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
Im sorry, but still this windows message is not capturing the keys.
My intention is to capture the Windows function key to disable the use of
WINDOWS + E key to open the Explorer in windows.
Re: Capture Windows KeyControls
Quote:
Originally Posted by madanmohany
you probably have the OK and Cancel Buttons on the dialog, and either of those buttons have the focus. . . .
Are you sure you posted in the right thread?
Re: Capture Windows KeyControls
Quote:
Originally Posted by danandu
By the way how to capture the Windows function key.
Window keys is defined as VK_LWIN and VK_RWIN.
Quote:
Originally Posted by danandu
My intention is to capture the Windows function key to disable the use of
WINDOWS + E key to open the Explorer in windows.
You will not be able to disable this key by handling it in your application. Windows gets it from low level keyboard handler.
Why would you want to disable this shortcut? Do you want to disable it when you application is running? Again why?
You would be able to do it by setting keyboard hook but if I would never use application that changes default Windows behavior.
Re: Capture Windows KeyControls
I have just noticed that you are actually bringing up three issues in this thread. First is taken care of.
The second:
Quote:
Originally Posted by danandu
I was trying to capture the keypress event for any key
To clarify, you are not capturing key press event. In the nutshell, when key is pressed (event) Windows generates WM_KEYDOWN , WM_KEYUP and WM_CHAR.
First two, carry key virtual code and for character keys only small caps code; would have to find out modifier (shift, alt, ctrl) keys are down by asking about keyboard state, to determine if this is 'A' or 'a'.
WM_CHAR, however translates this for you and brings you char code instead of key code, meaning that your comparison would distinguish the difference between 'A' and 'a'.
Get more information about those messages from MSDN.
Third issue:
Quote:
Originally Posted by danandu
changing the cursor state.
It is not a cursor state that you are changing but cursor itself.
SetCursor will change cursor briefly until next mouse movement. It will be changed to a default cursor, used when window class was registered.
If you want to permanently change windows’ cursor, register different class. You can also change cursor for the class by calling SetClassLong.
If you want to change cursor depending on certain circumstances, do it in WM_SETCURSOR message handler.
Declare member variable of the HCURSOR type. Assign different cursors as you desire and call SetCursor passing handle stored in this variable and returning TRUE from the handler.
If you want to allow default cursor return result of the base class WM_SETCURSOR handler (call base class OnSetCursor implementation).
I think now you have all three potatoes baked.
Re: Capture Windows KeyControls
You will need to handle WM_GETDLGCODE and return DLGC_WANTALLKEYS
Please read documenation on GETDLGCODE on msdn.
http://msdn.microsoft.com/en-us/libr...25(VS.85).aspx
Re: Capture Windows KeyControls
O, well, I must have been really tired yesterday.
Today I took another look at the first post and I realized that it is a dialog we all are talking about.
Dialog never receives a keyboard focus. Period.
Therefore my previous post regarding WM_KEYxx apply to all other windows not dialogs.
Setting cursor applies.
WM_GETDLGCODE message is not send to a dialog. It is send to windows controls to give a control ability to override system handling of certain keys.
For example, multiline edit control can request Enter key message to be delivered by the system. By default edit control does not receive edit key notification.