Can I disable keyboard input for a specific control?

Can I turn off keyboard input to a control? For example, ListView ? How can I do it? I tried to override KeyUp KeyDown events, but apparently it wasn’t?

IsEnabled is a good solution, however I only want to disable keyboard interaction and leave mouse interaction intact.

+4
source share
5 answers

The handling of the KeyDown too large, but you can handle the PreviewKeyDown event, and this should give you the behavior you are looking for:

 private void MyListBox_PreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = true; } 
+13
source

Dear maciek, the only tiger you need to do is use the OnKeyDown event, just do

 private void txtInput_KeyDown(object sender, KeyEventArgs e) { e.Handled = true; // user can input e.Handled = false; // user cannot input } 
+4
source

KeyDown usually works for me if you do the following in it:

 e.Handled = true; e.SuppressKeyPress = true; 

A more complete example with a practical application (disconnecting input from non-numeric characters): http://cccontrols.codeplex.com/SourceControl/changeset/view/34146#611536

However, John is making a good point. Any reason you would like to disable interaction with Control but not set Enabled = false ?

Edit: I just noticed a WPF tag. Not so sure about my answer as I hate WPF;)

+3
source

This is the goal of WebControl.Enabled = false; so that it does not respond to user input.

edit: now that the question has changed, disabling the control is no longer a solution. However, I think that a control that responds to mouse clicks rather than the keyboard does not work, not everyone prefers to use the mouse.

+1
source

KeyPressEventArgs.Handled : Gets or sets a value indicating whether a KeyPress event has been processed.

Boolean Property Value true if the event is being processed; otherwise false .

if you set e.Handled = true , the keyboard event will no longer be dispatched.

0
source

Source: https://habr.com/ru/post/1300590/


All Articles