C++ WM_KEYDOWN - 捕获按键引起的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6332364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:57:11  来源:igfitidea点击:

WM_KEYDOWN - capturing keypress causing event

c++winapikeypress

提问by CaptainProg

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.

我正在使用 Win32 API。有人问我正在使用什么框架,但我不知道。我正在使用 Visual C++,该程序是一个 Windows 程序。

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).

我想要做的就是在按下特定键时发生事件。在这个例子中,我使用了 's' 键,事件是一个被设置为 1 或 0 的整数;无论在按键时未设置为哪个(我会使用 bool 但我还不知道它是如何工作的)。

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?!

有人告诉我使用 GetKeyState(),然后告诉我这实际上不好。我也被告知要使用 WM_KEYDOWN 但无法弄清楚它是如何工作的......当然我所做的必须是绝对的基本编程(键盘输入>输出),但我无法清楚地解释它是如何工作的?!

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?

都不工作。有谁知道我如何实现 WM_KEYDOWN?

I am using a Windows Message Loop

我正在使用 Windows 消息循环

回答by MerickOWA

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.

如果您希望按键被活动窗口或对话框接收,即使在对话框/窗口的 WINPROC 中也可以像这样处理 WM_KEYDOWN。

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

对于非常相似的 DialogBox,您仍然会有一个 DLGPROC,它作为最后一个参数传递给 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;
}