发送Windows键键preSS到应用程序 [英] Send Windows key keypress into application

查看:139
本文介绍了发送Windows键键preSS到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何检测当前pressed关键?

(编辑:对于它的价值,这不是一个重复的......不知道为什么它被评选为此类)

我有,我们用在办公室跟踪花在项目中的时间一个小计时器应用程序。有一个启动和停止按钮,以及项目和任务领域。当我们去休息,吃午饭和其他的东西,我们停止计时器该项目,然后重新启动。这是一个重复的任务,通常涉及挖窗口从背后拿出其他一些Windows,然后休息后相同。

I have a small timer application that we use in the office to track the hours spent on a project. There is a start and stop button, and a project and task fields. When we go on break and to lunch and other things, we stop the timer for that project, then restart it. This is a repetitive task that generally involves digging the window out from behind several other Windows, then the same after the break.

我想要做的就是分配WindowKey + W的工作计时器应用程序,并将它不仅能带来计时器应用到前面和重点,但也有其切换的启动/停止。

What I want to do is assign WindowKey+W to the work timer application and have it not only bring the timer application to the front and focus it, but also have it toggle the Start/Stop.

我已经尝试了一些搜索,但我似乎无法缩小的例子,我想要什么。我不知道,你可以打开一个Windows快捷方式的属性,并指定一个快捷键来启动一个程序,(我猜?)如果你有一个应用程序已经打开,并设置为允许该程序只有一个实例,它会带来该程序前???也许..

I have tried a number of searches, but I can't seem to narrow down the examples to what I want. I do know that you can open the properties of a Windows shortcut and assign a shortcut key to launch a program, and (I guess?) if you have that app already open and it is set to allow only one instance of the program that it will bring that program to the front??? maybe..

反正..但这种方法不会接受WindowsKey的为有效的组合键。我不知道它是否能以某种方式通过该键组合中的程序。

Anyway.. but that method will not accept the WindowsKey as a valid key combo. And I don't know if it can somehow pass that key combo in to the program.

我AP preciate任何帮助或指导在这里!

I appreciate any help or direction here!!

编辑 - 回答更新

感谢您@huadianz的回答!我转换你的code到VB:

Thank you @huadianz for your answer! I converted your code to VB:

Public Const MOD_WIN As Integer = &H8
Public Const KEY_W As Integer = &H57

<DllImport("user32.dll")> _
Public Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As Integer, vlc As Integer) As Boolean
End Function

<DllImport("user32.dll")> _
Public Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean
End Function

Public Sub RegisterKeys()
    RegisterHotKey(Me.Handle, 1, MOD_WIN, KEY_W)
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)

    If (m.Msg = &H312) Then
        Me.TopMost = True
        Me.PlayPauseTimer()
        Me.TopMost = False
    End If
End Sub

在一个有趣的说明, Me.BringToFront()也不会真正实现应用程序到门前,在此情况下在Win7中,也没有我.Focus()。然而, Me.TopMost = TRUE 的工作,但它具有使窗口总在最前面的辅助效果。将其设置为,切换计时器,然后返回将其设置为的伟大工程!

On an interesting note, Me.BringToFront() would not actually bring the application to the front in this scenario in Win7, nor did Me.Focus(). However, Me.TopMost = True worked, but it has the secondary effect of making the window always on top. Setting it to True, toggling the timer, then setting it back to False works great!

推荐答案

如果你想完整的操作系统一体化,可以使用的PInvoke勾到内核的输入功能。

If you want full operating system intergration, you can hook into the kernel input functions by using PInvoke.

您正在寻找什么是explorer.exe的Windows API函数,在这里详细描述:

What you are looking for is the explorer.exe Windows API function, described in detail here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx

使用PInvoke的,你可以调用这个C ++函数

Using PInvoke, you can invoke this C++ function

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

public const int MOD_WIN = 0x00000008;
public const int KEY_W = 0x00000057

public static void RegisterKeys()
{
    RegisterHotKey((IntPtr)this.Handle, 1, MOD_WIN, KEY_W);
}

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if (m.Msg == 0x0312)
        this.Visible = !this.Visible;
}

这篇关于发送Windows键键preSS到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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