How to implement a global interception of keys (hook)?

In hook that I found on the Internet, there seems to be all clear and simple, but I don't get how to call it, and do not understand how to declare instead of the key with the "d" parameter with the Keys (In the main form NewForm), only one, the one that pushes into 2 form and stored in Storage.cs.

PS [HookKeyBoard] - All associated with the hook.

UPD: Downloaded all on Yandex.the drive round is-that is not at odds, and therefore chose the poison. I really hope that will help..
https://yadi.sk/d/RNTbfC9kfCXiZw

The hook
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SplashSpamKey
{
 class GlobalKeyHook
{
 /// <summary>
 /// Specifies the key modifiers.
 /// </summary>
[Flags]
 public enum KeyModifiers : uint
{
 /// <summary>
 /// Empty modifiers
 /// </summary>
 None = 0x0000,
 /// <summary>
 /// Either ALT key must be held down.
 /// </summary>
 Alt = 0x0001,
 /// <summary>
 /// Either CTRL key must be held down.
 /// </summary>
 Control = 0x0002,
 /// <summary>
 /// Either SHIFT key must be held down.
 /// </summary>
 Shift = 0x0004,
 /// <summary>
 /// Either WINDOWS key was held down. 
 /// These keys are labeled with the Windows logo. 
 /// Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system.
 /// </summary>
 Windows = 0x0008,
 //IgnoreAllModifier = 0x0400,
 //OnKeyUp = 0x0800,
 //MouseRight = 0x4000,
 //MouseLeft = 0x8000,
}

 //The hook [HookKeyBoard]

 public class HotKey : IMessageFilter, IDisposable
{
 #region Extern
 const int WM_HOTKEY = 0x312;
 const int ERROR_HOTKEY_ALREADY_REGISTERED = 0x581;

 [DllImport("user32.dll", SetLastError = true)]
 [return: MarshalAs(UnmanagedType.Bool)]
 static extern bool RegisterHotKey(IntPtr hWnd, IntPtr id, KeyModifiers fsModifiers, Keys vk);

 [DllImport("user32.dll", SetLastError = true)]
 [return: MarshalAs(UnmanagedType.Bool)]
 static extern bool UnregisterHotKey(IntPtr hWnd, IntPtr id);
#endregion

 private IntPtr windowHandle;
 public event HandledEventHandler Pressed;

 public HotKey()
 : this(Keys.None, KeyModifiers.None)
{
}

 public HotKey(Keys keyCode, KeyModifiers modifiers)
{
 this.KeyCode = keyCode;
 this.Modifiers = modifiers;
Application.AddMessageFilter(this);
}

~HotKey()
{
this.Dispose();
}

 public void Dispose()
{
 if (this.IsRegistered)
this.Unregister();

 this.windowHandle = IntPtr.Zero;
 this.Modifiers = KeyModifiers.None;
 this.KeyCode = Keys.None;
 this.Tag = 0;
}

 private bool OnPressed()
{
 HandledEventArgs e = new HandledEventArgs(false);
 if (this.Pressed != null)
 this.Pressed(this, e);

 return e.Handled;
}

 /// <summary>
 /// Filters out a message before it is dispatched.
 /// </summary>
 /// <param name="message">
 /// The message to be dispatched. You cannot modify this message.
 /// </param>
 /// <returns>
 /// true to filter the message and stop it from being dispatched;
 /// to false to allow the message to continue to the next filter or control.
 /// </returns>
 public bool PreFilterMessage(ref Message message)
{
 if (message.Msg != WM_HOTKEY || !this.IsRegistered)
 return false;

 if (message.WParam == this.Guid)
 return this.OnPressed();

 return false;
}

 /// <summary>
 /// Defines a system-wide hot key.
 /// </summary>
 /// <param name="windowControl">
 /// A handle to the window that will receive messages generated by the hot key. 
 /// </param>
 public void Register(Control window)
{
 if (this.IsRegistered)
 throw new NotSupportedException("You can't detect the hotkey is already occupied");
 if (this.IsEmpty)
 throw new NotSupportedException("You cannot register an empty hotkey");
 if (window.IsDisposed)
 throw new ArgumentNullException("window");

 this.windowHandle = window.Handle;
 if (!RegisterHotKey(this.windowHandle, this.Guid, this.Modifiers, this.KeyCode))
{
 if (Marshal.GetLastWin32Error() != ERROR_HOTKEY_ALREADY_REGISTERED)
 throw new Win32Exception();
}
 this.IsRegistered = true;
}

 /// <summary>
 /// Frees a hot key previously registered by the calling thread.
 /// </summary>
 public void Unregister()
{
 if (!this.IsRegistered)
return;

 if (!UnregisterHotKey(this.windowHandle, this.Guid))
 throw new Win32Exception();

 this.IsRegistered = false;
}

 public bool HasModifier(KeyModifiers modifiers)
{
 return (this.Modifiers & modifiers) != 0;
}

 public static HotKey Parse(object content)
{
 if (content == null)
 return new HotKey();

 return Parse(content.ToString());
}
 #region Fields

 private IntPtr Guid
{
 get { return new IntPtr((int)Modifiers << 16 | (int)KeyCode & 0xFFFF); }
}

 public bool IsEmpty
{
 get { return (this.KeyCode == Keys.None); }
}
 public bool IsRegistered { get; private set; }
 public KeyModifiers Modifiers { get; private set; }
 public Keys KeyCode { get; private set; }
 public int Tag { get; set; }
#endregion
}
}
}

The main form in which should happen a hook and emulation keystroke
private void NewForm_KeyDown(object sender, KeyEventArgs e)
{

 if (e.KeyValue == Storage.KeyData)
{
 if (Storage.IsEnabled == false)
{
 Storage.IsEnabled = true;
 Storage.RandValue = true;

 StatusOnOff.BackColor = Color.Green;
timer1.Start();

 //Example usage of the hook [HookKeyBoard] If it is not clear what kind of project and what it needs to do tacomania the example below and run the project
 var hkey = new HotKey(Keys.D, KeyModifiers.None); //Instead of "D" Need a button which sets the user (Stored in storage.cs)
 hkey.Pressed += (o, e) => { timer1.Start(); e.Handled = true; };
hkey.Register(this);
}
}
}
 private void NewForm_KeyUp(object sender, KeyEventArgs e)
{
 if (e.KeyValue == Storage.KeyData)
{
 if (Storage.IsEnabled == true)
{
 Storage.IsEnabled = false;
 Storage.RandValue = false;

 StatusOnOff.BackColor = Color.Red;
timer1.Stop();


}
}
}

 private void timer1_Tick(object sender, EventArgs e)
{
 Random rand = new Random();
 while (Storage.RandValue == true)
{
 int kk = rand.Next(100, 250);
 timer1.Interval = kk;
 SendKeys.SendWait("{" + Storage.KeyStr + "}");
}
 }

Form in which the user enters the key (which, when clamping will emulate their same-clicking on the timer) - 1 form
private void ChooseKeyForm_KeyDown(object sender, KeyEventArgs e)
{
 Storage.KeyStr = e.KeyCode.ToString();
 Storage.KeyData = e.KeyValue;
this.Dispose();
 }

Class which stores the variables, in order to transfer them between forms
internal class Storage {
 public static string KeyStr { get; set; }
 public static int KeyData { get; set; }
 public static bool IsEnabled { get; set; }
 public static bool RandValue { get; set; }
 }

To clarify what I need to do. The hook is catching the key presses for all keys with modifiers and in consequence of their registration. I also have to make a capture with a single key (the user chooses it) with modifiers (ALT,WIN,SHIFT,CTRL) - these modifiers are already written, so they do not need to touch. But I was faced with the problem of registration of this key, as the hook registration directly via Keys, and I need to make this check via that key, which is stored in the class Storage (which is set by the user)
April 7th 20 at 15:20
0 answer

Find more questions by tags C#KeyboardWindows Forms