سلام
شما نیاز به کلاس زیر دارید
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DCC_Utility
{
class Hotkeys
{
//http://www.dreamincode.net/forums/topic/180436-global-hotkeys/
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private int modifier;
private int key;
private IntPtr hWnd;
private int id;
public static class Constants
{
//modifiers
public const int NOMOD = 0x0000;
public const int ALT = 0x0001;
public const int CTRL = 0x0002;
public const int SHIFT = 0x0004;
public const int WIN = 0x0008;
//windows message id for hotkey
public const int WM_HOTKEY_MSG_ID = 0x0312;
}
public Hotkeys(int modifier, Keys key, Form form)
{
this.modifier = modifier;
this.key = (int)key;
this.hWnd = form.Handle;
id = this.GetHashCode();
}
public override int GetHashCode()
{
return modifier ^ key ^ hWnd.ToInt32();
}
public bool Register()
{
return RegisterHotKey(hWnd, id, modifier, key);
}
public bool Unregiser()
{
return UnregisterHotKey(hWnd, id);
}
}
}
به تعداد Hotkey های مورد نیاز متغیر در اول فرم خود تعریف کنید
Hotkeys ghk;
Hotkeys ghk1;
Hotkeys ghk2;
Hotkeys ghk3;
در constructor فرم مقادیر HotKey را تعریف نمایید
InitializeComponent();
ghk = new Hotkeys(Hotkeys.Constants.CTRL, Keys.OemSemicolon, this);
ghk1 = new Hotkeys(Hotkeys.Constants.CTRL, Keys.I, this);
ghk2 = new Hotkeys(Hotkeys.Constants.CTRL, Keys.O, this);
ghk3 = new Hotkeys(Hotkeys.Constants.CTRL, Keys.B, this);
این چهار روتین را به فرم خود اضافه نمایید
private Keys GetKey(IntPtr LParam)
{
return (Keys)((LParam.ToInt32()) >> 16); // not all of the parenthesis are needed, I just found it easier to see what's happening
}
protected override void WndProc(ref Message m)
{
if (m.Msg == Hotkeys.Constants.WM_HOTKEY_MSG_ID)
switch (GetKey(m.LParam))
{
//case Keys.OemSemicolon:
// HandleHotkey();
// break;
case Keys.I:
toolStripButton2_Click(null, null);
break;
case Keys.O:
toolStripButton3_Click(null, null);
break;
case Keys.B:
toolStripButton4_Click(null, null);
break;
case Keys.E:
toolStripButton5_Click(null, null);
break;
case Keys.D:
toolStripButton6_Click(null, null);
break;
case Keys.Q:
toolStripButton1_Click_1(null, null);
break;
case Keys.T:
tsUtility_Click(null, null);
break;
case Keys.K:
tsKawasaki_Click(null, null);
break;
}
base.WndProc(ref m);
}
private void RegHotKeys()
{
ghk.Register();
ghk1.Register();
ghk2.Register();
ghk3.Register();
ghk4.Register();
ghk5.Register();
ghk6.Register();
ghk7.Register();
ghk8.Register();
}
private void UnRegHotKeys()
{
ghk.Unregiser();
ghk1.Unregiser();
ghk2.Unregiser();
ghk3.Unregiser();
ghk4.Unregiser();
ghk5.Unregiser();
ghk6.Unregiser();
ghk7.Unregiser();
ghk8.Unregiser();
}
در ابتدای کلاس ریفرنس خودم را نوشته ام. باز هم اگر مشکل داشتید بفرمایید
موفق باشید