Как я могу зарегистрировать глобальную горячую клавишу, чтобы сказать CTRL+SHIFT+(LETTER), используя WPF и.NET 3.5?
Я строю приложение в C# с использованием WPF. Как я могу связать некоторые ключи?
Кроме того, как я могу привязать к ключу Windows?
11 ответов
Я не уверен, что вы подразумеваете под "глобальным" здесь, но здесь это так (я предполагаю, что вы имеете в виду команду на уровне приложения, например, " Сохранить все", которая может быть вызвана из любого места с помощью Ctrl + Shift + S.)
Вы найдете глобальный UIElement
на ваш выбор, например, окно верхнего уровня, которое является родителем всех элементов управления, где вам нужна эта привязка. Из-за "всплывающих" событий WPF события в дочерних элементах будут пузыриться вплоть до корня дерева элементов управления.
Теперь сначала вам нужно
- связать комбинацию клавиш с помощью команды, используя
InputBinding
как это - затем вы можете подключить команду к вашему обработчику (например, к коду, который вызывается
SaveAll
) черезCommandBinding
,
Для ключа Windows вы используете правильный элемент перечисления ключей, Key.LWin
или же Key.RWin
public WindowMain()
{
InitializeComponent();
// Bind Key
InputBinding ib = new InputBinding(
MyAppCommands.SaveAll,
new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control));
this.InputBindings.Add(ib);
// Bind handler
CommandBinding cb = new CommandBinding( MyAppCommands.SaveAll);
cb.Executed += new ExecutedRoutedEventHandler( HandlerThatSavesEverthing );
this.CommandBindings.Add (cb );
}
private void HandlerThatSavesEverthing (object obSender, ExecutedRoutedEventArgs e)
{
// Do the Save All thing here.
}
Это полное рабочее решение, надеюсь, оно поможет...
Использование:
_hotKey = new HotKey(Key.F9, KeyModifier.Shift | KeyModifier.Win, OnHotKeyHandler);
...
private void OnHotKeyHandler(HotKey hotKey)
{
SystemHelper.SetScreenSaverRunning();
}
Учебный класс:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mime;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace UnManaged
{
public class HotKey : IDisposable
{
private static Dictionary<int, HotKey> _dictHotKeyToCalBackProc;
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, UInt32 fsModifiers, UInt32 vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int WmHotKey = 0x0312;
private bool _disposed = false;
public Key Key { get; private set; }
public KeyModifier KeyModifiers { get; private set; }
public Action<HotKey> Action { get; private set; }
public int Id { get; set; }
// ******************************************************************
public HotKey(Key k, KeyModifier keyModifiers, Action<HotKey> action, bool register = true)
{
Key = k;
KeyModifiers = keyModifiers;
Action = action;
if (register)
{
Register();
}
}
// ******************************************************************
public bool Register()
{
int virtualKeyCode = KeyInterop.VirtualKeyFromKey(Key);
Id = virtualKeyCode + ((int)KeyModifiers * 0x10000);
bool result = RegisterHotKey(IntPtr.Zero, Id, (UInt32)KeyModifiers, (UInt32)virtualKeyCode);
if (_dictHotKeyToCalBackProc == null)
{
_dictHotKeyToCalBackProc = new Dictionary<int, HotKey>();
ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(ComponentDispatcherThreadFilterMessage);
}
_dictHotKeyToCalBackProc.Add(Id, this);
Debug.Print(result.ToString() + ", " + Id + ", " + virtualKeyCode);
return result;
}
// ******************************************************************
public void Unregister()
{
HotKey hotKey;
if (_dictHotKeyToCalBackProc.TryGetValue(Id, out hotKey))
{
UnregisterHotKey(IntPtr.Zero, Id);
}
}
// ******************************************************************
private static void ComponentDispatcherThreadFilterMessage(ref MSG msg, ref bool handled)
{
if (!handled)
{
if (msg.message == WmHotKey)
{
HotKey hotKey;
if (_dictHotKeyToCalBackProc.TryGetValue((int)msg.wParam, out hotKey))
{
if (hotKey.Action != null)
{
hotKey.Action.Invoke(hotKey);
}
handled = true;
}
}
}
}
// ******************************************************************
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
// ******************************************************************
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be _disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be _disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!this._disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
Unregister();
}
// Note disposing has been done.
_disposed = true;
}
}
}
// ******************************************************************
[Flags]
public enum KeyModifier
{
None = 0x0000,
Alt = 0x0001,
Ctrl = 0x0002,
NoRepeat = 0x4000,
Shift = 0x0004,
Win = 0x0008
}
// ******************************************************************
}
Регистрация ярлыков на уровне ОС вряд ли когда-либо будет полезной: пользователи не хотят, чтобы вы связывались с их ОС.
Тем не менее, есть гораздо более простой и удобный способ сделать это в WPF, если вы согласны с сочетанием клавиш, работающим только внутри приложения (т. Е. До тех пор, пока основное внимание уделяется вашему приложению WPF):
В App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(Window), Window.PreviewKeyUpEvent, new KeyEventHandler(OnWindowKeyUp));
}
private void OnWindowKeyUp(object source, KeyEventArgs e))
{
//Do whatever you like with e.Key and Keyboard.Modifiers
}
Это так просто
Если вы собираетесь смешивать Win32 и WPF, вот как я это сделал:
using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media;
using System.Threading;
using System.Windows;
using System.Windows.Input;
namespace GlobalKeyboardHook
{
public class KeyboardHandler : IDisposable
{
public const int WM_HOTKEY = 0x0312;
public const int VIRTUALKEYCODE_FOR_CAPS_LOCK = 0x14;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private readonly Window _mainWindow;
WindowInteropHelper _host;
public KeyboardHandler(Window mainWindow)
{
_mainWindow = mainWindow;
_host = new WindowInteropHelper(_mainWindow);
SetupHotKey(_host.Handle);
ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
}
void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled)
{
if (msg.message == WM_HOTKEY)
{
//Handle hot key kere
}
}
private void SetupHotKey(IntPtr handle)
{
RegisterHotKey(handle, GetType().GetHashCode(), 0, VIRTUALKEYCODE_FOR_CAPS_LOCK);
}
public void Dispose()
{
UnregisterHotKey(_host.Handle, GetType().GetHashCode());
}
}
}
Вы можете получить код виртуальной клавиши для горячей клавиши, которую хотите зарегистрировать, здесь: http://msdn.microsoft.com/en-us/library/ms927178.aspx
Может быть, есть и лучший способ, но это то, что у меня так далеко.
Ура!
Это похоже на уже даные ответы, но я нахожу это немного чище:
using System;
using System.Windows.Forms;
namespace GlobalHotkeyExampleForm
{
public partial class ExampleForm : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public ExampleForm()
{
InitializeComponent();
int id = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); // Register Shift + A as global hotkey.
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
}
}
private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
}
}
}
Я нашел это на fluxbytes.com.
С пакетом NHotKey вы можете сделать свою горячую клавишу глобальной:
- https://github.com/thomaslevesque/NHotkey
- https://thomaslevesque.com/2014/02/05/wpf-declare-global-hotkeys-in-xaml-with-nhotkey/ (используйте web.archive.org, если ссылка не работает)
Короче говоря, для XAML все, что вам нужно сделать, это заменить
<KeyBinding Gesture="Ctrl+Alt+Add" Command="{Binding IncrementCommand}" />
по
<KeyBinding Gesture="Ctrl+Alt+Add" Command="{Binding IncrementCommand}"
HotkeyManager.RegisterGlobalHotkey="True" />
Я не уверен насчет WPF, но это может помочь. Я использовал решение, описанное в RegisterHotKey (user32) (конечно, измененное в соответствии с моими потребностями) для приложения Windows Forms на C#, чтобы назначить комбинацию CTRL-KEY в Windows для вызова формы C#, и оно прекрасно работало (даже в Windows Vista), Надеюсь, это поможет и удачи!
Я нашел Глобальные горячие клавиши в проекте WPF на codeproject.com, который делает эту работу за меня. Это относительно недавно, не нуждается в ссылке на System.Windows.Forms и работает "глобально" с точки зрения реакции на нажатие горячей клавиши, даже если "ваше" приложение не является активным окном.
Решение Baboon работает лучше всего, потому что у вас может быть несколько окон. Я настроил его так, чтобы он использовал PreviewKeyDownEvent вместо PreviewKeyUpEvent для обработки повторения нажатий клавиш.
Я бы посоветовал не регистрироваться на уровне ОС, если вы не пишете что-то наподобие снайперского инструмента или приложения для записи звука, так как оно позволит вам получить доступ к функциям, когда окно не сфокусировано.
Хотя RegisterHotKey иногда именно то, что вы хотите, в большинстве случаев вы, вероятно, не хотите использовать общесистемные горячие клавиши. В итоге я использовал код, подобный следующему:
используя System.Windows; using System.Windows.Interop; пространство имен WpfApp { открытый частичный класс MainWindow: Window { const int WM_KEYUP = 0x0101; const int VK_RETURN = 0x0D; const int VK_LEFT = 0x25; public MainWindow () { this.InitializeComponent (); ComponentDispatcher.ThreadPreprocessMessage + = ComponentDispatcher_ThreadPreprocessMessage; } void ComponentDispatcher_ThreadPreprocessMessage (ref MSG msg, ref bool обработано) { if (msg.message == WM_KEYUP) { if ((int)msg.wParam == VK_RETURN) MessageBox.Show("RETURN был нажат"); if ((int) msg.wParam == VK_LEFT) MessageBox.Show ("LEFT был нажат"); } } } }
Сотрудник написал пример того, как создать низкоуровневую клавиатуру для использования с WPF.
http://blogs.vertigo.com/personal/ralph/Blog/Lists/Posts/Post.aspx?ID=8
RegisterHotKey()
предложенный Джоном мог бы работать - единственный улов - то, что это требует HWND (используя PresentationSource.FromVisual()
и приведение результата к HwndSource).
Тем не менее, вам также нужно будет ответить на WM_HOTKEY
сообщение - я не уверен, есть ли способ получить доступ к WndProc окна WPF или нет (что можно сделать для окон Windows Forms).