2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • +1 Your question & answer was usefull for me. Thanks. – Jonathan Nov 12 '09 at 20:47
  • If you have a message box in the method call, things might not work as expected. See [cannot-disable-beep-sound-on-textbox-keydown-event](http://stackoverflow.com/questions/19219597/cannot-disable-beep-sound-on-textbox-keydown-event) – nawfal Feb 06 '14 at 14:13

3 Answers3

5

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)

Kara
  • 6,115
  • 16
  • 50
  • 57
tamberg
  • 1,987
  • 14
  • 23
1

Disregard, I was able to solve the problem by suppressing the keycode right from the start without affecting it's normal functions. Here is what I used:

e.SuppressKeyPress = (e.KeyCode == e.KeyCode);
Dmitry
  • 13,797
  • 6
  • 32
  • 48
0

You need to set e.Handled = true; in order to prevent the keystroke from bubbling up.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 3
    HI I had the same problem, e.Handled = true didn't solve the problem. e.SuppressKeyPress = true did it. – Jonathan Nov 12 '09 at 20:49