java wmkeydown,WM_KEYDOWN-捕获按键事件

I am trying to do a very simple task - have an event occur when a key is pressed - but am having a lot of difficulty implementing it.

I am using the Win32 API. I have been asked what framework I am using but I don't know that. I am using Visual C++ and the program is a Windows program.

All I want to do is have an event occur if a specific key is pressed. For this example I am using the 's' key, and the event is an integer either being set to 1 or 0; whichever it wasn't set to at the time of the key press (I would use bool but I don't know how it works just yet).

I have been told to use GetKeyState(), and then told that this is actually no good. I have also been told to use WM_KEYDOWN but can't work out how this works... Surely what I am doing must be absolute basic programming (keyboard input > output) but I can't get a clear explanation as to how it works?!

I have tried using the following, with no luck:

int Flag;

if (GetKeyState(115) == 1 && Flag == 0) Flag = 1;

if (GetKeyState(115) == 1 && Flag == 1) Flag = 0;

I have also tried using this:

if (GetKeyState(115) & 0x8000 && Flag == 0) Flag = 1;

if (GetKeyState(115) & 0x8000 && Flag == 1) Flag = 0;

Neither work. Does anyone know how I could implement WM_KEYDOWN?

I am using a Windows Message Loop

解决方案

There are several ways to solve this problem. None of which will give you "nano-second" accuracy but here they are.

If you want the keypress to be recieved by an active window or dialog you handle a WM_KEYDOWN even in the WINPROC of the dialog/window like so.

void InSomePlace()

{

WNDCLASS wndClass

ZeroMemory( &wndClass, sizeof(wndClass) );

// Initialize wndClass members here

wndClass.lpszClassName = _T("MyWindow");

wndClass.lpfnWndProc = &MyWndProcHandler; //

RegisterClass( &wndClass );

HWND hWnd = CreateWindow( _T("MyWindow", /* lots of other parameters */ );

MSG msg;

BOOL bRet;

while ( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0 )

{

if (bRet == -1)

{

// handle the error and possibly exit

}

else

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

}

LRESULT CALLBACK MyWndProcHandler( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )

{

switch ( uMsg )

{

// Lots of case statements, in particular you want a WM_KEYDOWN case

case WM_KEYDOWN:

if ( wParam == 'S' )

{

// Do something here

return 0L;

}

break;

}

return DefWindowProc( hwnd, uMsg, wParam, lParam );

}

For a DialogBox its very similar, you would still have a DLGPROC which is passed as the last parameter to DialogBox/CreateDialog

void InSomePlace( HINSTANCE hInstance, HWND hParentWindow )

{

DialogBox( hInstance, _T("MyDialogTemplate"), hParentWindow, &MyDialogProc );

}

INT_PTR CALLBACK MyDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )

{

case ( uMsg )

{

// Lots of case statements, in particular you want a WM_KEYDOWN case

case WM_KEYDOWN:

if ( wParam == 'S' )

{

// Do something here

SetWindowLong(hwndDlg, DWL_MSGRESULT, 0L);

return TRUE;

}

break;

}

return FALSE;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值