3

I wounder if there is a way to check if a key is clicked?

What I want to achieve is creating something like text editor, I have finished most of the parts. Now when I press on a button, I have a function under element PreviewKeyUp and the other PreviewKeyDown ... Is there something like PreviewKeyClick or KeyClick?

PS: I'm using visual studio 2010 and compiling against .NET Framework 3.5 ... I know I can use 4.0 but 3.5 is due to restrictions on client PC. The application targets a specific client and I can't update the .NET Framework on the client machine. If more details is needed, don't hesitate to ask.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
sikas
  • 5,435
  • 28
  • 75
  • 120
  • possible duplicate of [Can I enable PreviewClick using InputBindings in WPF?](http://stackoverflow.com/questions/2590924/can-i-enable-previewclick-using-inputbindings-in-wpf) – Conrad Frix May 11 '11 at 20:06
  • @Conrad Frix: not a duplicate, the one you`re referring to is for listViews!! not for textBoxes and keyboard button clicks – sikas May 11 '11 at 20:11

2 Answers2

2

KeyPress event is what you are looking for I think. Warning: It does not work for non-character keys.

soandos
  • 4,978
  • 13
  • 62
  • 96
  • I can`t find KeyPress event in the list I see when I press "K" ... I can find KeyUp and KeyDown Only!! – sikas May 11 '11 at 20:10
  • http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(VS.90).aspx I do not know why you can't see it, but it is there – soandos May 11 '11 at 20:11
  • I guess it is not supported in WPF! – sikas May 11 '11 at 20:25
  • @sikas Correct. In WPF you'd probably want to use [KeyDown](http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown.aspx). – Dan J May 11 '11 at 21:08
2

In general, for text editors you would need to handle TextInput (or PreviewTextInput) and KeyDown/PreviewKeyDown events. The latter handles keys not included in TextInput events.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • It worked! Now how can I read the backspace, the spacebar and the return keys in addition to the other keys? – sikas May 11 '11 at 20:24
  • 1
    @sikas - I believe you'd need to use KeyDown for things like Backspace/Delete, but general text input should be handled via TextInput. TextInput is smart about various things, like entering non-ASCII characters. Different languages have different rules/input. So typing "ab" in English will be "ab", but in another language it may result in an "a" with an accent/tilde on top. – CodeNaked May 11 '11 at 20:30