简体   繁体   English

有什么方法可以在VB.NET中捕获睡眠键(按钮)?

[英]There's any way to capture sleep key(button) in VB.NET?

As in the event Form Closing I want to show a msgbox asking if I really want to put PC in to sleep mode and cancel the sleep order if the user answer "No". 与“表单关闭”事件一样,我想显示一个msgbox,询问我是否真的要使PC进入睡眠模式,如果用户回答“否”,则取消睡眠顺序。

I need to detect this even if the application is minimize or out of focus. 即使应用程序最小化或焦点模糊,我也需要检测到这一点。

Thanks. 谢谢。

I don't have a sleep button on my keyboard, so I can't verify, but this might be worth a test... 我的键盘上没有睡眠按钮,因此无法验证,但这可能值得一试...

In your active forms KeyDown event, put something like this: 在活动表单KeyDown事件中,输入以下内容:

Private Sub form_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)Handles Me.KeyDown

    Select Case e.KeyCode
        Case Keys.Sleep
            'Do something when the sleep button is pressed

    End Select
End Sub

If you want the keypress to not get sent to the active control in the form, add 如果您不想将按键发送到表单中的活动控件,请添加

e.Handled = True

I have learnt that in order for the main form to capture keypresses, the "KeyPreview" property of the form must be set to "True". 我了解到,为了使主窗体能够捕获按键,必须将窗体的“ KeyPreview”属性设置为“ True”。 If not the keypress will be sent to the control with focus. 如果不是,则将按键发送给具有焦点的控件。

I have also seen that there are differences in what keys can be detected in the KeyDown event and the KeyPress event. 我还看到在KeyDown事件和KeyPress事件中可以检测到哪些键有所不同。 As an example, you can test for an arrow key in the KeyDown event, but not in the KeyPress event, as they are using a different event argument type, so it is worth checking both if you can't find the key you are looking for. 例如,您可以在KeyDown事件中测试箭头键,但不能在KeyPress事件中测试箭头键,因为它们使用的是不同的事件参数类型,因此值得一提的是,如果找不到您要查找的键,对于。

As to making it work when the program is not in focus, that appears to be more difficult. 至于在程序不集中时使其工作,这似乎更加困难。 The following link shows how to register a HotKey which you can trap even when your program is not in focus: http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET However, looking at the available key-codes to trap, I can not fins a Sleep or Power option. 以下链接显示了如何注册即使您的程序不在焦点时也可以捕获的热键: http : //www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET但是,查看可用的陷阱代码,我无法查找“睡眠”或“电源”选项。 They can be found here: http://msdn.microsoft.com/en-us/library/ms927178.aspx 可以在这里找到它们: http : //msdn.microsoft.com/en-us/library/ms927178.aspx

As I said, I can't verify that this works, but if you can, make a comment so other people will know. 就像我说的那样,我无法验证这是否可行,但是如果可以,请发表评论,以便其他人知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM