NO

Author Topic: Compound Virtual Keys  (Read 3519 times)

tpekar

  • Guest
Compound Virtual Keys
« on: February 13, 2013, 05:38:14 PM »
Does anyone know how to capture compound virtual keys in windows such as Ctrl_x or Alt_z?

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Compound Virtual Keys
« Reply #1 on: February 14, 2013, 12:17:37 AM »
Does anyone know how to capture compound virtual keys in windows such as Ctrl_x or Alt_z?

Set a flag when handling the WM_KEYDOWN or WM_KEYUP messages for the Ctrl or Alt key.
Or use GetKeyState, VK_CONTROL in the WM_CHAR handler. It really depends on your specific needs.

tpekar

  • Guest
Re: Compound Virtual Keys
« Reply #2 on: February 15, 2013, 07:08:25 PM »
Thanks for the tip.  I'll give it a try.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Compound Virtual Keys
« Reply #3 on: February 16, 2013, 09:52:27 AM »
Actually, I am not sure if GetAsyncKeyState is not a better choice. Both seem to work just fine.

This is not C but you'll understand it anyway:
   SWITCH uMsg
   CASE WM_CHAR
      invoke GetAsyncKeyState, VK_CONTROL
      .if sword ptr ax<0
         or wParam, 64
         PrintLine "Ctrl ", Chr$(wParam)
      .else
         PrintLine Chr$(wParam)
      .endif


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Compound Virtual Keys
« Reply #4 on: February 16, 2013, 11:18:40 AM »
In GUI program you can use Accelerator's
Here is example without recources.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define ID_CTRL_X 6000
#define ID_ALT_Z 6001

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

TCHAR *szAppName = TEXT("WinTestAccel");
TCHAR *szFrameClass = TEXT("cWinTestAccel");
HWND hFrame;
HANDLE hInst;
HACCEL hAccel;
ACCEL aAccel[] = {
{FCONTROL | FVIRTKEY, 0x58, ID_CTRL_X},
{FALT | FVIRTKEY, 0x5A, ID_ALT_Z}
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;

wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC)WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)COLOR_3DSHADOW;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = szFrameClass;
wcx.hIconSm = 0;

if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;

hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if (!hFrame)
return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);

hAccel = CreateAcceleratorTable(aAccel, 2);
while (GetMessage(&msg, NULL, 0, 0)) {
if (TranslateAccelerator(msg.hwnd, hAccel, &msg))
continue;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyAcceleratorTable(hAccel);
return msg.wParam;
}

void TestKB(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
//      case WM_CREATE:
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_CTRL_X:
SetWindowText(hWnd, TEXT("Ctrl-X"));
break;
case ID_ALT_Z:
SetWindowText(hWnd, TEXT("Alt-Z"));
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return (0);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
May the source be with you