c# - 除特定程序外,如何注册 Windows 全局热键?
问题描述
问题
我创建了一个“Foo”WPF 应用程序,只要在 Windows 10 操作系统的任何位置按下热键 ctrl + shift + 空格键,它就会显示其主窗口。这工作正常。
现在,出现了问题。使用 Visual Studio (2019),快捷键之一也是 ctrl + shift + 空格键(当光标位于方法括号之间时显示重载参数)。在使用 VS 时按下此热键时,将显示“Foo”窗口而不是重载参数列表。
因此,我想在 Visual Studio 的“Foo”应用程序中创建一个例外,以便在 Visual Studio 中按下此热键时,显示重载参数,而不是“Foo”应用程序。实现这一目标的最佳方法是什么?是否有一个特殊的 RegisterHotkey 参数可以做到这一点,或者在“Foo”窗口出现之前检查活动窗口是否是 VS 更好?谢谢。
当前工作应用程序的代码片段
Imports System.Windows.Interop
Imports System.Runtime.InteropServices
Public Class Hotkey
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum
Public NotInheritable Class User32
Private Sub New()
End Sub
<DllImport("user32.dll")>
Friend Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As ModifierKeys, vk As RecentWorkFolders.Keys.Keys) As Boolean
End Function
<DllImport("user32.dll")>
Friend Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean
End Function
End Class
<Flags>
Public Enum Modifiers
NoMod = &H0
Alt = &H1
Ctrl = &H2
Shift = &H4
Win = &H8
End Enum
Public Sub InitializeHotkey()
Dim windowHandle As IntPtr = New WindowInteropHelper(MainWindowInstance).Handle
Dim result As Boolean = User32.RegisterHotKey(windowHandle, 1, ModifierKeys.Control Or ModifierKeys.Shift, RecentWorkFolders.Keys.Keys.Space)
End Sub
Public Sub HookWndProc(window As Visual)
Dim source = TryCast(PresentationSource.FromVisual(window), HwndSource)
If source Is Nothing Then
Console.WriteLine("Could not create hWnd source from window.")
End If
source.AddHook(AddressOf WndProc)
End Sub
Private Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
If msg = &H312 Then
handleHotKeyEvent(wParam)
End If
Return IntPtr.Zero
End Function
Public Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
Select Case hotkeyID
Case 1
IsWindowShown = False
Initialize()
ShowWindow()
End Select
End Sub
结束类
解决方案
推荐阅读
- php - 在 null 上调用成员函数 isCollection()
- python - 单击按钮时,Python Flask 停止处理路由
- amazon-web-services - Puppet 代理在 AWS 上的 Ubuntu 16.04 的 POSIX 语言环境中运行
- ruby-on-rails - 什么轨道需要宝石?
- c# - 使用文本框控件asp.net插入数据库
- r - R中的序列组合矩阵
- sql - 使用 Group By 会影响平均值
- ios - iOS动画绘制多个圆圈,只显示一个
- rust - 如何在盒装的 Future 上调用“and_then”?
- javascript - 覆盖 document.write