473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

C# Registered HotKey Down / Up event

Tyler Wiebe
I'm wondering how I can handle a registered hot key in a way other then when it's pressed.

The following will continually fire the HotKeyPressed event if the hot key is held down.
Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : System.Windows.Forms.Form
  2.     {
  3.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  4.         private static extern bool RegisterHotKey(System.IntPtr hWnd, int id, int fsModifiers, int vk);
  5.  
  6.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  7.         private static extern bool UnregisterHotKey(System.IntPtr hWnd, int id);
  8.  
  9.         public delegate void HotKeyPressedHandler();
  10.         public event HotKeyPressedHandler HotKeyPressed;
  11.  
  12.         public enum Modifiers : uint
  13.         {
  14.             ALT = 0x0001,
  15.             CTRL = 0x0002,
  16.             SHIFT = 0x0004,
  17.             WIN = 0x0008,
  18.         }
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             this.HotKeyPressed += new HotKeyPressedHandler(Form1_HotKeyPressed);
  24.             RegisterHotKey(this.Handle, 0, (int)Modifiers.CTRL, (int)System.Windows.Forms.Keys.Q);
  25.         }
  26.  
  27.         void Form1_HotKeyPressed()
  28.         {
  29.             System.Console.WriteLine("Hot Key Is Pressed");
  30.         }
  31.  
  32.         protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  33.         {
  34.             UnregisterHotKey(this.Handle, 0);
  35.             base.OnClosing(e);
  36.         }
  37.         protected override void WndProc(ref System.Windows.Forms.Message m)
  38.         {
  39.             if (m.Msg == 0x312 && this.HotKeyPressed != null) this.HotKeyPressed();
  40.             base.WndProc(ref m);
  41.         }
  42.     }
  43.  
I want to know if there is a way to only fire the HotKeyPressed event when the hot key is first pressed, like a HotKeyDown event, and also I wouldn't mind having a HotKeyUp event.

So if anyone knows how to make it only fire once per press, please let me know.
Jul 8 '12 #1
0 1750

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nick | last post by:
I have a windows application wherein I have focus on a datagrid. Now when i hit the down arrow, I want to capture the event. However, my datagrid.KeyDown event handle is not getting invoked at all....
2
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely...
0
by: Jax | last post by:
Say for example, you have a panel, within the panel are various controls Right forget the controls, what you want is the co-ordinates of where you click on the panel Problem is that when a control...
3
by: orekinbck | last post by:
Hi How do I catch a SHIFT TAB in the key down event of a text box? Thanks Bill
1
by: Marina | last post by:
Hi, Is there a way to find out if any event handlers are registered for a given event of a given object?
1
by: Boni | last post by:
Dear all, I use picturebox keydown event. Unfortunately when I press and hold shift key this event fires many times. Is there a posibility to have an event, which fires only once when key...
3
by: ±è¿ë°Ç | last post by:
i used a list view component to a form and i would like to do some thing when i click right mouse on the list view's column header but the mouse down event was not occured while i click list...
1
by: Ben | last post by:
Hi I would like to complete a task whenever F4 is pressed by the user anywhere on a form. The problem is the Key Down event does not fire for the form if the user is, for example within a...
1
by: JDeats | last post by:
If you create a new C# Windows Application project in Visual Studio.NET 2003 or 2005 and proceed to. 1. Make default form size 800x600 2. Drop a CheckBoxList control and dock it to the left 3....
1
by: karaballo | last post by:
Hi All, How to trap arrow key down event on MDIChild form? Form alone works fine without MDI container. I think, ToolStrip stole these arrows key down events. How I can redirect arrow key down...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.