WM_KEYDOWN - 捕获引起事件的按键

发布于 2024-11-15 17:03:07 字数 779 浏览 2 评论 0原文

我正在尝试执行一项非常简单的任务 - 当按下某个键时会发生一个事件 - 但实现它有很多困难。

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

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

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

我尝试过使用以下方法,但没有成功:

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

我也尝试过使用这个:

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

两者都不起作用。有谁知道我如何实现 WM_KEYDOWN?

我正在使用 Windows 消息循环

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-11-22 17:03:07

有几种方法可以解决这个问题。这些都不会给你“纳秒”的精度,但它们就在这里。

如果您希望活动窗口或对话框接收按键,您甚至可以在对话框/窗口的 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 );
}

对于 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文