Recent

Author Topic: OnKeyUp error sound  (Read 1728 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1177
OnKeyUp error sound
« on: February 04, 2023, 12:24:14 pm »
When some key combinations are pressed, Windows emits error sound.
In OnKeyDown event one can suppress that by assigning Key := 0;.
This doesn't work in OnKeyUp.
How to prevent this error sound when I catch key combination in OnKeyUp?

Lazarus 2.2.4, Windows 11.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: OnKeyUp error sound
« Reply #1 on: February 04, 2023, 12:53:13 pm »
That event is just too late for it.
"Prevent" you can only in OnKeyDown or OnKeyPress (KeyPreview must be True)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: OnKeyUp error sound
« Reply #2 on: February 04, 2023, 12:58:44 pm »
That event is just too late for it.
"Prevent" you can only in OnKeyDown or OnKeyPress (KeyPreview must be True)
Wrong. Since OnKeyPress  and OnKeyDown are repeating events. OnKeyUp isn't.
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: OnKeyUp error sound
« Reply #3 on: February 04, 2023, 01:55:03 pm »
Hmmm, I do not know how you check to prevent Sound in "Up" cause there the keys already pressed and released so I ask for your way of doing please @Thaddy
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: OnKeyUp error sound
« Reply #4 on: February 04, 2023, 02:23:29 pm »
That event is just too late for it.
"Prevent" you can only in OnKeyDown or OnKeyPress (KeyPreview must be True)
Wrong. Since OnKeyPress  and OnKeyDown are repeating events. OnKeyUp isn't.

Exactly, other two are repeating and I'd like to use OnKeyUp. But not with that sound.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: OnKeyUp error sound
« Reply #5 on: February 04, 2023, 02:40:50 pm »
That event is just too late for it.
"Prevent" you can only in OnKeyDown or OnKeyPress (KeyPreview must be True)
Wrong. Since OnKeyPress  and OnKeyDown are repeating events. OnKeyUp isn't.

Exactly, other two are repeating and I'd like to use OnKeyUp. But not with that sound.
I am excited to learn how to prevent it too, in an event where its already happened.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: OnKeyUp error sound
« Reply #6 on: February 04, 2023, 03:05:25 pm »
Hi!

The solution is in
https://windowsreport.com/fix-keyboard-beeping/


1. Disable non-plug and play driver

    Right click Start.
    Select Device Manager.
    Locate the View tab and click on it.
    Choose Show Hidden Devices.
    A drop down list with the devices will come up.
    Click Non-Plug and Play Drivers.
    Right click Beep option and click Properties.
    Select Driver tab and hit Disable.
    Click Apply then OK.



Winni

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: OnKeyUp error sound
« Reply #7 on: February 04, 2023, 03:22:40 pm »
Hi!

The solution is in
https://windowsreport.com/fix-keyboard-beeping/


1. Disable non-plug and play driver

    Right click Start.
    Select Device Manager.
    Locate the View tab and click on it.
    Choose Show Hidden Devices.
    A drop down list with the devices will come up.
    Click Non-Plug and Play Drivers.
    Right click Beep option and click Properties.
    Select Driver tab and hit Disable.
    Click Apply then OK.



Winni

I don't have 'Non-Plug and Play Drivers' so I can't do that.
But even if this works, it's not good solution for me. I control my program, not Windows settings.
So, I probably can't use OnKeyUp.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: OnKeyUp error sound
« Reply #8 on: February 04, 2023, 03:32:33 pm »
That event is just too late for it.
"Prevent" you can only in OnKeyDown or OnKeyPress (KeyPreview must be True)
Wrong.
So, I probably can't use OnKeyUp.
Since he say that my answer is wrong you still have hope! Not give up that quick, be patient, he's a very active helping and doing cool!

@Winni  :D :D :D Great Answer that is!! You made my day more shiny :D :D :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: OnKeyUp error sound
« Reply #9 on: February 04, 2023, 03:40:59 pm »
What exactly is the magic key combination that is causing this issue?

The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: OnKeyUp error sound
« Reply #10 on: February 04, 2023, 03:49:13 pm »
What exactly is the magic key combination that is causing this issue?

Ctrl + P, Ctrl + O, Ctrl + N, ..., Ctrl + Enter, maybe more.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: OnKeyUp error sound
« Reply #11 on: February 04, 2023, 03:49:39 pm »
What exactly is the magic key combination that is causing this issue?
Ctrl + P, Ctrl + O, Ctrl + N, ..., Ctrl + Enter, maybe more.
CTRL don't raise sounds, that would be new to me.
On Windows you can nearly press ALT+AnyLetter = Sound, doesn't need to be specific. ( That's a feature by Microsoft :P )
« Last Edit: February 04, 2023, 03:51:43 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

paweld

  • Hero Member
  • *****
  • Posts: 966
Re: OnKeyUp error sound
« Reply #12 on: February 04, 2023, 03:50:22 pm »
If you want, you can stay with OnKeyUp, only that you still need OnKeyDown.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. var
  3.   tmpkey: Word;
  4. begin
  5.   if ((ssCtrl in Shift) and not (ssAlt in Shift)) or (not (ssCtrl in Shift) and (ssAlt in Shift)) then
  6.   begin
  7.     tmpkey := Key;
  8.     Key := 0;
  9.     If (Shift = [ssCtrl]) and (tmpkey = VK_O) then
  10.       Caption := Caption + ' d';
  11.   end;
  12. end;
  13.  
  14. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  15. begin
  16.   If (Shift = [ssCtrl]) and (Key = VK_P) then
  17.     Caption := Caption + ' u';
  18. end;
  19.  
Best regards / Pozdrawiam
paweld

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: OnKeyUp error sound
« Reply #13 on: February 04, 2023, 08:48:34 pm »
What exactly is the magic key combination that is causing this issue?
Ctrl + P, Ctrl + O, Ctrl + N, ..., Ctrl + Enter, maybe more.
CTRL don't raise sounds, that would be new to me.
On Windows you can nearly press ALT+AnyLetter = Sound, doesn't need to be specific. ( That's a feature by Microsoft :P )

Ctrl doesn't raise sounds, but Ctrl + letters does. Ctrl is Strg on German keyboards.

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: OnKeyUp error sound
« Reply #14 on: February 04, 2023, 08:51:45 pm »
If you want, you can stay with OnKeyUp, only that you still need OnKeyDown.

Thanks for that. I assumed (wrongly) that if I set Key to 0 in OnKeyDown, that OnKeyUp won't be raised (because obviously Key is used somewhere after OnKeyDown, to produce sound of course :), but it is also passed to controls).

Based on your idea, I came up with this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ProcessKey(var Key: Word; Shift: TShiftState; const AOnKeyDown: Boolean);
  2. var bKeyProcessed: Boolean = True;
  3. begin
  4.   If Shift = [ssCtrl] then
  5.     case Key of
  6.       VK_O: If AOnKeyDown then Caption := Caption + ' d';
  7.       VK_P: If not AOnKeyDown then Caption := Caption + ' u';
  8.     otherwise
  9.       bKeyProcessed := False;
  10.     end
  11.   else
  12.     bKeyProcessed := False;
  13.  
  14.   If bKeyProcessed and AOnKeyDown then
  15.     Key := 0;
  16. end;
  17.  
  18. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  19. begin
  20.   ProcessKey(Key, Shift, True);
  21. end;
  22.  
  23. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  24. begin
  25.   ProcessKey(Key, Shift, False);
  26. end;

 

TinyPortal © 2005-2018