输入密钥未发生KeyDown_Event [英] KeyDown_Event is not occuring for Enter Key

查看:75
本文介绍了输入密钥未发生KeyDown_Event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,Mewara,

我在vs2008中将vb.net与窗口应用程序一起使用.
我已遵循此代码.对于Esc键或发生事件,它工作正常.
但是当我使用此代码进行Enter键事件时却没有得到..

Hi S Mewara,

I m using vb.net with window application in vs2008.
I have followed this code. it is working fine for Esc key or event is occur.
but when i have used this code for enter key event is not getting..

Private Sub frmScreen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close() 
        ElseIf e.KeyCode = Keys.Enter Then
            'btnPriview_Click(sender, e)
            MessageBox.Show("Fire Enter")
        End If
    End Sub



1. e.KeyCode = Keys.Escape-按下Esc键时可以正常工作.
2. e.KeyCode = Keys.Enter-按下Enter键时,它不起作用.
如何解决这个问题.......
建议我

谢谢
穆克什

[edit]已添加代码块-OriginalGriff [/edit]



1. e.KeyCode = Keys.Escape - it is working when Esc key is down.
2. e.KeyCode = Keys.Enter - it is not working when Enter key is down..

How to resolve this problem.......
suggest me

Thanks
mukesh

[edit]Code block added - OriginalGriff[/edit]

推荐答案

假设密钥确实输入了frmScreen_KeyDown子项,请检查以下内容:

Assuming that the key does enter the frmScreen_KeyDown sub, check for:

e.KeyCode = Keys.Return


它比您想象的要复杂一些-它还取决于您对表单具有哪些控件.
如果创建空白表格并处理Key Down事件,则将获取所有键,输入(如e.Return),ESC(如e.Escape)等.

如果向其添加按钮,则该事件突然停止,因为该按钮可以接受输入并因此具有焦点-它获取所有Key事件而不是表单.因此,您还需要将处理程序添加到所有控件中-没关系,已经将相同的处理程序添加到了所有控件中,没有任何实际问题.

但这也不能解决您的问题,因为某些控件(例如按钮)不仅会接受输入,而且会吞下某些键:例如,一个标准按钮将吞下Enter键并产生Click事件.取消按钮通常会吞下Escape键,并产生Click事件.

如果我是您,我会重新考虑键盘的使用-使用标准框架键的用法要比颠覆键盘的操作更容易-而且对于可能期望键像其他操作一样进行操作的用户来说,操作也更容易应用程序.
It is slightly more complex than you think - it also depends on what controls you have on your form.
If you create a blank form, and handle the Key Down event, you will get all the keys, enter (as e.Return), ESC (as e.Escape), etc.

If you add a button to it, then suddenly the event stops, because the button can take input and thus has the focus - it gets all the Key events instead of the form. So, you need to add the handler to all the controls as well - that''s ok, the same handler had be added to them all without any real problem.

But that won''t solve your problem either, because some controls (buttons for example) will not only accept input, but will swallow some keys: A standard button for example will swallow the Enter key and produce a Click event. A Cancel button will normally swallow a Escape key, and produce a Click event.

If I were you, I would re-think my keyboard usage - it is easier to work with the standard framework key usage, than the subvert it - and it is easier for your users who may expect keys to do things as they do in other applications.


这篇关于输入密钥未发生KeyDown_Event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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