Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - How to prevent the beep sound caused by alt key pressed in a WinForms TextBox?

I'm creating a routine that allows the user to replicate keyboard shortcuts into a textbox for 'custom keyboard shortcuts' customization, but every time the alt key is pressed with another letter, it produces another sound.

I'm capturing the keys in the textbox_keydown event to parse the modifiers + other keys into a readable Shift + A or Ctrl + Shift + B manner into that very same textbox. Should I be doing this in a different event like textbox_previewkey instead of textbox_keydown? How can I prevent the alt modifier key + a letter or number causing the Beep sound?

the textbox is just a normal .net 3.5 textbox with the only edited properties of it being the ReadOnly property to false. Is there a better way I could re-do this?

I'm currently just checking that if any modifiers keys are pressed and then + a-z or 0-9, then to go ahead and input the appropriately pressed keys into that same textbox like Shift + A or Ctrl + Shift + Y.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Handle the KeyDown event of the TextBox, e.g. with void HandleKeyDown(object source, KeyEventArgs e) and inside enter: if (e.Alt) e.SuppressKeyPress = true;.

(also works for the Return key)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...