无法抑制按键 [英] unable to supress keys

查看:89
本文介绍了无法抑制按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在C#Windows应用程序中使用一个RichTextBox.在某些情况下,我需要限制用户从键盘输入任何键.因此,我在的RichTextBox的keydown事件中编写了以下代码行.

Hi all,

I am using one RichTextBox in C# Windows Application. I need to restrict the user from entering any key from keyboard at certain conditions. so, I wrote the following line of code in keydown event of RichTextBox at .

e.SuppressKeyPress = true;


这对于英语美式键盘工作正常.但是,当我将区域设置更改为朝鲜语并将键盘语言更改为朝鲜语时,即使执行了上述语句,我也可以输入朝鲜语字符(例如뮤아ㅓㅑㅇ).

即使我尝试使用以下代码


This is working fine for English US keyboard. But when i changed my regional settings to Korea and keyboard language to Korean, i can enter the Korean characters (like 뮤아ㅓㅑㅇ) even after executing the above statement.

even i have tried with the following code

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


但徒劳无功.

请帮忙.
提前非常感谢.


but in vain.

Please help.
Thanks a lot in advance.

推荐答案

很遗憾,您没有提供相关信息.可能是System.Windows.Forms.处理事件KeyPress或重写虚拟方法OnKeyPress并使用KeyPressEventArgs.Handled抑制键.

请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.keypresseventargs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].

—SA
Too bad you did not provide relevant information. It''s probably System.Windows.Forms. Handle the event KeyPress or override the virtual method OnKeyPress and use KeyPressEventArgs.Handled to suppress the key.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

—SA


您是否尝试过处理控制键的事件?也许这就是系统如何查看这些键的方式.您所做的是正确的,应该可以工作.按下这些键时,事件是否会触发?是否设置了断点以查看?
Have you tried handling the events for control keys ? Perhaps that''s how the system views those keys. What you have done is correct, and should work. Do the events fire at all when you press those keys, have you set a breakpoint to see ?


您可以使用 KeyPress 事件来抑制所需的键.
e.KeyChar 属性给出了字符的Unicode码.
以下代码段可用于取消所需的键.
You can use the KeyPress event to suppress the required keys.
The e.KeyChar property gives the unicode number of the character.
The following code snippet can be used to suppress the required keys.
int MinKeyCode = 2309; // Enter the minimum key code here
int MaxKeyCode = 2320; // Enter the maximum key code here
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) {

    if (e.KeyChar >= MinKeyCode && e.KeyChar <= MaxKeyCode)
    {
        e.Handled = true;
    }
}


您可以使用以下代码找出您要禁止显示的字符的unicode号


You can use the following code to find out the unicode number of the characters you want to suppress

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) {
    listBox1.Items.Add(e.KeyChar + " - " + (int)e.KeyChar);
}


这篇关于无法抑制按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆