880231 - overriding keyboard behaviour
-
hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx
-
hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx
-
hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx
The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...
BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_UP)
{
CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
if (prev) prev->SetFocus();
return TRUE;
}
else if (pMsg->wParam == VK_DOWN)
{
CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
if (next) next->SetFocus();
return TRUE;
}
}return CDialog::PreTranslateMessage(pMsg);
}(tested with VS2010 Beta 1 - woo-hoo!)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...
BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_UP)
{
CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
if (prev) prev->SetFocus();
return TRUE;
}
else if (pMsg->wParam == VK_DOWN)
{
CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
if (next) next->SetFocus();
return TRUE;
}
}return CDialog::PreTranslateMessage(pMsg);
}(tested with VS2010 Beta 1 - woo-hoo!)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
this is a good method. indeed doing what the dialog itself does in handling these keys again in our code for another keys. but when there're many dialogs in the program and i'm not going to derive them from a common base and put this code in its PreTranslateMessage method makes it a bit not a straightforward method. the code i've to write is what the others also r working on. all keyboard handlings must occur in the application's PreTranslateMessage. i know that it's not a logical limitation, but this is what i can do.
-
The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...
BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_UP)
{
CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
if (prev) prev->SetFocus();
return TRUE;
}
else if (pMsg->wParam == VK_DOWN)
{
CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
if (next) next->SetFocus();
return TRUE;
}
}return CDialog::PreTranslateMessage(pMsg);
}(tested with VS2010 Beta 1 - woo-hoo!)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
please notice the following code i put in the application's PreTranslateMessage:
... case VK_DOWN: pMsg->wParam=VK_TAB; BYTE byKeybState[256]; GetKeyboardState(byKeybState); byKeybState[VK_SHIFT] = !(BOOL)GetKeyState(VK_SHIFT); // or TRUE SetKeyboardState(byKeybState); break; ...
it must work. right? but in CE it seems that GetKeyboardState and SetKeyboardState functions r not available. what can i do? -
please notice the following code i put in the application's PreTranslateMessage:
... case VK_DOWN: pMsg->wParam=VK_TAB; BYTE byKeybState[256]; GetKeyboardState(byKeybState); byKeybState[VK_SHIFT] = !(BOOL)GetKeyState(VK_SHIFT); // or TRUE SetKeyboardState(byKeybState); break; ...
it must work. right? but in CE it seems that GetKeyboardState and SetKeyboardState functions r not available. what can i do?I'd use GetAsncKeyState[^] (which is in CE) and the SendInput technique documented in the other answer. The following pseudo-code shows the approach I'd use:
if (GetAsyncKeyState shows Shift is pressed)
{
use SendInput to send { Tab key press, Tab key release };
}
else
{
use SendInput to send { Shift key press, Tab key press, Tab key release, Shift key release };
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I'd use GetAsncKeyState[^] (which is in CE) and the SendInput technique documented in the other answer. The following pseudo-code shows the approach I'd use:
if (GetAsyncKeyState shows Shift is pressed)
{
use SendInput to send { Tab key press, Tab key release };
}
else
{
use SendInput to send { Shift key press, Tab key press, Tab key release, Shift key release };
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
thank u :) i think this is the solution i was looking for. i'm going to implement it.
-
thank u :) i think this is the solution i was looking for. i'm going to implement it.
yeah, that's it! thx :)