If you want to unsubscribe from keypress event:
_globalHook = Hook.GlobalEvents();
_globalHook.KeyPress += GlobalHookKeyPress; //Subscribe
_globalHook.KeyPress -= GlobalHookKeyPress; //Unsubscribe
Edit:
I understand now that you called OnCombination.
After going over the code of this method, you cannot change the combinations list after you created it. Other calls to OnCombination will just add more registrations.
Edit:
Another option is to use reactive extensions:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reactive.Linq;
using Gma.System.MouseKeyHook;
using MouseKeyHook.Rx;
namespace HotkeyPlay
{
public partial class Form1 : Form
{
private IDisposable _keysObservable;
public Form1()
{
InitializeComponent();
var triggers = new Trigger[]
{
Trigger.On(Keys.H).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
private void button1_Click(object sender, EventArgs e)
{
_keysObservable.Dispose();
var triggers = new Trigger[]
{
Trigger.On(Keys.B).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
}
}
Nuget packages:
Install-Package System.Reactive.Linq
Install-Package MouseKeyHook.Rx
MouseKeyHook.Rx is still in pre-release version.