![]() |
![]() |
News... | Hack-Acad | Downloads | Web-Projekte | System-Check | Kontakt |
HACKACAD - C# - Register Hot Key Ein "Hot Key" ist ein Systemweites Tastaturkürzel, dass bedeutet egal wo der Hot Key gedrückt wird, ruft er das zugewiesene Programm auf. Die Grundlage für einen gültigen HK ist SHIFT, STRG, ALT oder WIN TASTE in Kombination mit einem Buchstaben. Ebenso funktionieren auch Kombination von SHIFT, STRG, ALT und WIN. Diese Tasten sind als INT Wert definiert: /* * MOD_ALT 0x0001 * MOD_CONTROL 0x0002 * MOD_SHIFT 0x0004 * MOD_WIN 0x0008 */ Ergo ist die Kombination von CONTROL (STRG) und der SHIFT Taste 2 + 4 = 6. BEISPIEL CODE Dieses Tutorial wird einen HotKey (ALT + Y) für ein leeres Formular registrieren. Eine HKReceiver Klasse wird als Empfänger für den HotKey dienen und das Formular ein- und ausblenden. Als erstes das leere Formular. |
using System; using System.ComponentModel; using System.Windows.Forms; namespace CreateHotKey { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } } } |
||
Als nächstes die HotKey Receiver Klasse. | ||
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace CreateHotKey { public class HKReceiver : System.Windows.Forms.Form { public HKReceiver() { IntPtr hwnd = this.Handle; setHotKey(hwnd, 0x0001, 'Y'); } public static bool setHotKey(IntPtr Handle, int mods, char letter) { // 666 kann hier ein belieber Wert sein // identifiziert lediglich den HotKey return RegisterHotKey(Handle,666,mods, (int)letter); } /* Notwendige API Imports */ [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); public const int WM_HOTKEY = 0x0312; /* Methode zum Empfangen der WM_HK Nachricht */ protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_HOTKEY: //Steuerklasse informieren SteuerKlasse.ShowHide(); break; default: //MessageBox.Show("default"); break; } base.WndProc(ref m); } } } |
||
Zum Schluss noch die SteuerKlasse: | ||
using System; using System.Windows.Forms; namespace CreateHotKey { public class SteuerKlasse : System.Windows.Forms.Form { static Form1 f1 = new Form1(); static void Main() { HKReceiver hkr = new HKReceiver(); f1.ShowDialog(); Application.Run(); } public static void ShowHide() { if (f1.Visible) f1.Visible = false; else f1.Visible = true; } } } |
||