I'm making a windows form application and I need to detect if a key is pressed (Globally), so I ended up with using hotkeys and everything works fine. I registered key 'V' as the hotkey and when I press it my app can detect it. But the problem is when I press V while I'm holding 'Shift' or 'Control' , my app won't detect the key press anymore.
I found a way to also register modifier keys, but I can only register one combination , and if I register multiple combinations like :"V" ,"Control + V" ,and "Shift + V" , only the last one works but I want my app to detect 'V' in any combination.
Here is the code I use for Registering hotkeys(Found on the internet)
private class KeyHandler
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
private readonly int _key;
private readonly IntPtr _hWnd;
private readonly int _id;
private readonly int _modifier;
public KeyHandler(Keys key, KeyModifiers modifier, Form form)
{
_key = (int)key;
_modifier = (int)modifier;
_hWnd = form.Handle;
_id = GetHashCode();
}
public sealed override int GetHashCode()
{
return _key ^ _hWnd.ToInt32();
}
public void Register()
{
RegisterHotKey(_hWnd, _id, _modifier, _key);
}
}
public enum KeyModifiers : int
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Win = 8
}
private static int WM_HOTKEY = 0x0312;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
HandleHotkey();
}
base.WndProc(ref m);
}
//On Press
private void HandleHotkey()
{
}
private void Form1_Load(object sender, EventArgs e)
{
//Register hotkey
new KeyHandler(Keys.V, 0, this).Register();
}
Is there any way to detect a key press globally even if the key is pressed with modifiers?
Thanks to Hans Passant's comment, I found this source code on Github which allows you to capture keyboard events.
https://github.com/rvknth043/Global-Low-Level-Key-Board-And-Mouse-Hook
I have a application that runs in the background like i may keep my app in the system tray.If it remains on system tray my app will do it's job. Whenever a user press F10 or F9 some works will be done. i tried this:
public partial class Form1 : Form
{
public int a = 1;
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);
const int MYACTION_HOTKEY_ID = 1;
public Form1()
{
InitializeComponent();
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int) Keys.F9);
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int)Keys.F10);
this.ShowInTaskbar = false;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.F9) == -32767))
{
if ((a % 2) != 0)
{
a++;
MessageBox.Show(a.ToString()+"not equal F9");
label1.Text = "not equal F9";
}
if ((a % 2) == 0)
{
a++;
MessageBox.Show(a.ToString()+"equal F9");
label1.Text = " equal F9";
}
}
else if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.F10) == -32767))
{
if ((a % 2) != 0)
{
a++;
MessageBox.Show(a.ToString() + "not equal F10");
label1.Text = "not equal F10";
}
if ((a % 2) == 0)
{
a++;
MessageBox.Show(a.ToString() + "equal F10");
label1.Text = " equal F10";
}
}
base.WndProc(ref m);
}
}
As i use set "this.ShowInTaskbar = false" this line it doesn't work.But if i don't set this it works fine.For my app i have to use this line.How can i solve this????
You need to subscribe to certain messages that the operating system sends by means of a native function call like RegisterHotKey(). When you call this function You tell the operating system which window to send the messages to by specifying the Handle of the window, this can be considered an address. When you set ShowInTaskbar = false the handle changes so the operating system will not know where to reach you.
See the first arugment:
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int) Keys.F9);
To resolve your issue you can create a class that derives from NativeWindow which "Provides a low-level encapsulation of a window handle and a window procedure." and from within that class (or at least using that class's handle depending on your implementation), register the hotkeys using a handle that will never change.
public sealed class HotkeyManager : NativeWindow, IDisposable
{
public HotkeyManager()
{
CreateHandle(new CreateParams());
}
protected override void WndProc(ref Message m)
{
if (m.Msg == Constants.WM_HOTKEY)
{
//handle hotkey message
}
base.WndProc(ref m);
}
public void Dispose()
{
DestroyHandle();
}
}
As far as I know, you need to re-register the hotkey whenever you change the "ShowInTaskbar" state.
Someone else had a similar problem; see this thread.
I made an application that is able to change its app.config (the connection string part). I tried several solutions and this proved to be the easiest way to solve one of my problems. This is the code I use:
ConnectionStringSettings postavke = new ConnectionStringSettings("Kontrolor.Properties.Settings.KontrolorConnectionString", constring);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Clear();
config.ConnectionStrings.ConnectionStrings.Add(postavke);
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection(config.ConnectionStrings.SectionInformation.SectionName);
This code is placed inside a button_click method, and when I click that button and restart the application the changes are visible.
My question is this - is there a way to do it from another (independent) application that would enable the user to create the connection string by entering the required values into a textBox or selecting it from comboBox (he needs only to enter the IP of the server and the name of the database). By doing that, the first application would be preprepared and there would be no need to restart it to apply changes.
Is there a way to do this?
Since both applications are on same machine you can use simple windows messaging, register windows message in both applications and sender post message to receiver, here is the example code:
Sender :
public partial class FormSender : Form
{
[DllImport("user32")]
private static extern int RegisterWindowMessage(string message);
private static readonly int WM_REFRESH_CONFIGURATION = RegisterWindowMessage("WM_REFRESH_CONFIGURATION");
[DllImport("user32")]
private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
public FormSender()
{
InitializeComponent();
}
private void btnNotify_Click(object sender, EventArgs e)
{
NotifyOtherApp();
}
private void NotifyOtherApp()
{
List<Process> procs = Process.GetProcesses().ToList();
Process receiverProc = procs.Find(pp => pp.ProcessName == "Receiver" || pp.ProcessName == "Receiver.vshost");
if (receiverProc != null)
PostMessage((IntPtr)receiverProc.MainWindowHandle, WM_REFRESH_CONFIGURATION, new IntPtr(0), new IntPtr(0));
}
}
Receiver :
public partial class FormReceiver : Form
{
[DllImport("user32")]
private static extern int RegisterWindowMessage(string message);
private static readonly int WM_REFRESH_CONFIGURATION = RegisterWindowMessage("WM_REFRESH_CONFIGURATION");
public FormReceiver()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_REFRESH_CONFIGURATION)
{
lblMessageReceived.Text = "Refresh message recevied : " + DateTime.Now.ToString();
}
else
{
base.WndProc(ref m);
}
}
}
btw. note that I am checking for process name "Receiver.vshost" so that it can work when started in VS debugger
Note:
Using Windows Forms
preferably C# .NET
Question:
Best method for implementing a drag-over-window tool, similar (or identical) to that featured in process explorer, to obtain the process ID corresponding to the selected Window
I think the easiest way is to put a control on your form that acts as a starting point; you press a mouse button there, and then you move it over the screen while the button is pressed, and pick up the process ID of whatever you are pointing at. I my example I have used a panel (called _aim).
First we set up the mouse events:
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
// make all mouse events being raised in the _aim panel
// regardless of whether the mouse is within the control's
// bounds or not
_aim.Capture = true;
}
private void Panel_MouseMove(object sender, MouseEventArgs e)
{
if (_aim.Capture)
{
// get the process id only if we have mouse capture
uint processId = GetProcessIdFromPoint(
_aim.PointToScreen(e.Location)).ToString();
// do something with processId (store it for remembering the
// last processId seen, to be used as MouseUp for instance)
}
}
private void Panel_MouseUp(object sender, MouseEventArgs e)
{
if (_aim.Capture)
{
// release capture if we have it
_aim.Capture = false;
// perhaps do something more (fetch info about last seen
// process id, if we stored it during MouseMove, for instance)
}
}
The GetProcessIdFromPoint method looks like this:
private uint GetProcessIdFromPoint(Point point)
{
uint procId;
WinApi.GetWindowThreadProcessId(WinApi.WindowFromPoint(point), out procId);
return procId;
}
And finally the windows API things (from pinvoke.net):
public static class WinApi
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
public static implicit operator System.Drawing.Point(POINT p)
{
return new System.Drawing.Point(p.X, p.Y);
}
public static implicit operator POINT(System.Drawing.Point p)
{
return new POINT(p.X, p.Y);
}
}
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(POINT Point);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
I'm building an application in C# using WPF. How can I bind to some keys?
Also, how can I bind to the Windows key?
This is a full working solution, hope it helps...
Usage:
_hotKey = new HotKey(Key.F9, KeyModifier.Shift | KeyModifier.Win, OnHotKeyHandler);
...
private void OnHotKeyHandler(HotKey hotKey)
{
SystemHelper.SetScreenSaverRunning();
}
Class:
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
}
// ******************************************************************
}
I'm not sure of what you mean by "global" here, but here it goes (I'm assuming you mean a command at the application level, for example, Save All that can be triggered from anywhere by Ctrl + Shift + S.)
You find the global UIElement of your choice, for example, the top level window which is the parent of all the controls where you need this binding. Due to "bubbling" of WPF events, events at child elements will bubble all the way up to the root of the control tree.
Now, first you need
to bind the Key-Combo with a Command using an InputBinding like this
you can then hookup the command to your handler (e.g. code that gets called by SaveAll) via a CommandBinding.
For the Windows Key, you use the right Key enumerated member, Key.LWin or Key.RWin
public WindowMain()
{
InitializeComponent();
// Bind Key
var ib = new InputBinding(
MyAppCommands.SaveAll,
new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control));
this.InputBindings.Add(ib);
// Bind handler
var 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.
}
Registering OS level shortcuts is hardly ever a good thing: users don't want you to mess with their OS.
That said, there is a much simpler and user friendly way of doing this in WPF, if you're ok with the hotkey working within the application only (i.e as long as your WPF app has the focus):
In 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
}
It's that simple
If you're going to mix Win32 and WPF, here's how I did it:
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());
}
}
}
You can get the virtual-key code for the hotkey you want to register here: http://msdn.microsoft.com/en-us/library/ms927178.aspx
There may be a better way, but this is what I've got so far.
Cheers!
This is similar to the answers already given, but I find it a bit cleaner:
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.
}
}
}
I've found it on fluxbytes.com.
With the NHotKey package, you can make your hotkey global:
https://github.com/thomaslevesque/NHotkey
https://thomaslevesque.com/2014/02/05/wpf-declare-global-hotkeys-in-xaml-with-nhotkey/ (use web.archive.org if the link is broken)
In short, for XAML, all you need to do is to replace
<KeyBinding Gesture="Ctrl+Alt+Add" Command="{Binding IncrementCommand}" />
by
<KeyBinding Gesture="Ctrl+Alt+Add" Command="{Binding IncrementCommand}"
HotkeyManager.RegisterGlobalHotkey="True" />
I'm not sure about WPF, but this may help. I used the solution described in RegisterHotKey (user32) (modified to my needs of course) for a C# Windows Forms application to assign a CTRL-KEY combination within Windows to bring up a C# form, and it worked beautifully (even on Windows Vista). I hope it helps and good luck!
I've found the Global Hotkeys in WPF project on codeproject.com which does the job for me. It's relatively recent, does not need a reference to System.Windows.Forms and works "globally" in terms of reacting to the hotkey being pressed even if "your" application is not the active window.
Baboon's solution works best because you may have multiple windows. I did tweak it so it uses the PreviewKeyDownEvent instead of the PreviewKeyUpEvent in order to handle repetition in keystrokes.
I would advise against OS-level registration unless you are writing something like a snipping tool or an audio recording app as it will let you access functionality when the window is not focused.
Although RegisterHotKey is sometimes precisely what you want, in most cases you probably do not want to use system-wide hotkeys. I ended up using code like the following:
using System.Windows;
using System.Windows.Interop;
namespace WpfApp
{
public partial class 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 handled)
{
if (msg.message == WM_KEYUP)
{
if ((int)msg.wParam == VK_RETURN)
MessageBox.Show("RETURN was pressed");
if ((int)msg.wParam == VK_LEFT)
MessageBox.Show("LEFT was pressed");
}
}
}
}
RegisterHotKey() suggested by John could work - the only catch is that it requires an HWND (using PresentationSource.FromVisual(), and casting the result to an HwndSource).
However, you'll also need to respond to the WM_HOTKEY message - I'm not sure if there is a way to get access to the WndProc of a WPF window or not (which can be done for Windows Forms windows).