private void ButtonRemapResetTableHotkey_Click(object sender, EventArgs e) { //Temporarily disable hotkeys settingsController.DisableHotkeys(); //Enable key preview KeyPreview = true; //Create temporary event handler to handle key preview void disableKeyPreview(object s, EventArgs eA) { settingsTabControl.SelectedIndexChanged -= disableKeyPreview; VisibleChanged -= disableKeyPreview; KeyPreview = false; //Enable hotkeys again settingsController.EnableHotkeys(); } //Set it so key preview gets disabled when the tab is changed settingsTabControl.SelectedIndexChanged += disableKeyPreview; //Set it so key preview gets disabled when the window is made invisible VisibleChanged += disableKeyPreview; //Create temporary event handler to handle the actual key void getPressedKey(object s, KeyEventArgs eA) { switch (eA.KeyCode) { case Keys.ControlKey: case Keys.ShiftKey: case Keys.Menu: case Keys.LWin: case Keys.RWin: //We ignore controls other than to combine them with others return; } //Remove the event handlers KeyDown -= getPressedKey; settingsTabControl.SelectedIndexChanged -= disableKeyPreview; VisibleChanged -= disableKeyPreview; KeyPreview = false; settingsController.RemapResetTableHotkey(eA.Modifiers, eA.KeyCode); //Enable hotkeys again settingsController.EnableHotkeys(); } KeyDown += getPressedKey; }