Home > Enterprise >  Remove Windows Error/Beep sound when pressing Alt Key combinations
Remove Windows Error/Beep sound when pressing Alt Key combinations

Time:02-03

I am trying to remove windows error sound from my Flutter (Win32) Application. After some research I came up with this fix. I tried this fix but it's not helping in my Flutter application.

Heres the code to handle WM_SYSCHAR message:

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;
  }
  ...
}

When I press the Alt Space, "SYSCHAR from win32" is printed in the console. But whenever I press any other key with Alt, this is not printed and the Windows error sound is played. It seems like SYSCHAR message is handled somewhere else?

This can be used to know the working and initialization of Win32 App in Flutter.

I just want to tell the Application that Alt Key combinations are handled and it doesn't have to play Windows error sound.

CodePudding user response:

Thanks to @IInspectable for suggesting me to use keyboard accelerators.

The problem is Flutter's main loop doesn't have keyboard accelerators. So I followed how to use keyboard accelerators documentation and modified the main loop as follows:

  1. Created accelerator table by calling CreateAcceleratorTable
LPACCEL accels = GenerateAccels();
HACCEL haccel = CreateAcceleratorTable(accels, 36);
if (haccel==NULL) {
  return EXIT_FAILURE;
}

Here's the 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. Then adding a call to TranslateAccelerator in the main loop
::MSG msg = { };
while (::GetMessage(&msg, nullptr, 0, 0) > 0) {
  if (!TranslateAccelerator(msg.hwnd, haccel, &msg)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }
}
  1. I also added this check to prevent error sound from playing when any key is pressed after pressing Alt (Alt is not held down).

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

Note: One thing that isn't working is Alt Alphabet combinations. When pressed, it is still playing that error sound. In my case it's not important right now, but if someone finds the fix then please share.

  • Related