簡體   English   中英

按 Alt + 組合鍵時刪除 Windows 錯誤/蜂鳴聲

[英]Remove Windows Error/Beep sound when pressing Alt + Key combinations

我正在嘗試從我的 Flutter (Win32) 應用程序中刪除 windows 錯誤聲音。 經過一些研究,我想出了這個修復方法 我嘗試了此修復程序,但它對我的 Flutter 應用程序沒有幫助。

下面是處理WM_SYSCHAR消息的代碼:

LRESULT CALLBACK Win32Window::WndProc(HWND const window,
                                      UINT const message,
                                      WPARAM const wparam,
                                      LPARAM const lparam) noexcept {
  if (message == WM_SYSCHAR) {
    std::cout << "SYSCHAR from win32" << std::endl;
    return 0;
  }
  ...
}

當我按下 Alt+Space 時,“SYSCHAR from win32”會打印在控制台中。 但是每當我用 Alt 按下任何其他鍵時,都不會打印出來,並且會播放 Windows 錯誤聲音。 似乎SYSCHAR消息在其他地方處理?

可以用來了解Flutter中Win32 App的工作和初始化。

我只想告訴應用程序已處理 Alt+Key 組合,它不必播放 Windows 錯誤聲音。

感謝@IInspectable建議我使用鍵盤加速器

問題是 Flutter 的主循環沒有鍵盤加速器。 所以我按照how to use keyboard accelerts 文檔修改了主循環如下:

  1. 通過調用CreateAcceleratorTable創建加速器表
LPACCEL accels = GenerateAccels();
HACCEL haccel = CreateAcceleratorTable(accels, 36);
if (haccel==NULL) {
  return EXIT_FAILURE;
}

這是GenerateAccels function:

LPACCEL GenerateAccels() {
  LPACCEL lpAccel = new ACCEL[36];
  
  // Alt + Number combinations:
  for (int i = 0; i < 10; i++) {
    lpAccel[i].fVirt = FALT;
    lpAccel[i].key = (WORD)(0x30 + i);
  }

  // Alt + Alphabet combinations (NOT WORKING AT THE MOMENT):
  for (int i = 0; i < 26; i++) {
    lpAccel[i + 10].fVirt = FALT;
    lpAccel[i + 10].key = (WORD)(0x41 + i);
  }

  return lpAccel;
}
  1. 然后在主循環中添加對TranslateAccelerator的調用
::MSG msg = { };
while (::GetMessage(&msg, nullptr, 0, 0) > 0) {
  if (!TranslateAccelerator(msg.hwnd, haccel, &msg)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }
}
  1. 我還添加了此檢查以防止在按下 Alt 后按下任何鍵時播放錯誤聲音(未按住 Alt)。

flutter_window.cpp

 case WM_SYSCOMMAND: // If the selection is in menu // handle the key event // This prevents the error/beep sound if (wparam == SC_KEYMENU) { return 0; }

注意:一件事不起作用是Alt + 字母組合。 按下時,它仍在播放錯誤聲音。 就我而言,現在這並不重要,但如果有人找到了解決方法,請分享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM