Can I disable keyboard input to a specific control

2019-05-07 01:05发布

Is it possible to disable keyboard input to a control? For instance a ListView? How do I do that? I've tried overriding the KeyUp KeyDown events but apparently that was not the way?

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

5条回答
Animai°情兽
2楼-- · 2019-05-07 01:07

That is the purpose of the WebControl.Enabled = false; to prevent it from responding to user input.

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

查看更多
我只想做你的唯一
3楼-- · 2019-05-07 01:13

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

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

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

John makes a good point though. Any reason you would want to disable interaction with the Control but not set Enabled = false?

Edit: I just noticed the WPF tag. Not so sure of my answer anymore since I'm a WPF hater ;-)

查看更多
淡お忘
4楼-- · 2019-05-07 01:22

Handling the KeyDown event is too late, but you can handle the PreviewKeyDown event and that should give you the behavior you are looking for:

private void MyListBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
   e.Handled = true;
}
查看更多
ゆ 、 Hurt°
5楼-- · 2019-05-07 01:27

KeyPressEventArgs.Handled : Gets or sets a value indicating whether the KeyPress event was handled.

Property Value Boolean true if the event is handled; otherwise, false.

if you set e.Handled = true, keyboard event not dispatch anymore.

查看更多
Evening l夕情丶
6楼-- · 2019-05-07 01:34

Dear maciek, the only thig you need to do is using OnKeyDown event just do

private void txtInput_KeyDown(object sender, KeyEventArgs e)
    {
            e.Handled = true; // user can input
            e.Handled = false; // user cannot input
    }
查看更多
登录 后发表回答