改键程序

用C#做了个改键软件,留个纪念。

//超人改键 by Luangeng
using System;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace superman {
public partial class Form1 : Form {
private bool openhook = true; private int[] text = new int[14]; privateint hHook= 0; privateint win= 1;
private RegistryKey k1, k2;
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, IntPtr threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll")]
private static extern IntPtr IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]
private static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); private HookProc hp ;
public Form1() {
Ini tializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
k1 = Registry.CurrentUser;
k2 = k1.CreateSubKey("Software\WarcraftKeyChange");
try {
}
text[0] = (int)k2.GetValue("1"); text[1] = (int)k2.GetValue("2"); text[2] = (int)k2.GetValue("3"); text[3] = (int)k2.GetValue("4"); text[4] = (int)k2.GetValue("5"); text[5] = (int)k2.GetValue("6"); text[6] = (int)k2.GetValue("7"); text[7] = (int)k2.GetValue("8"); text[8] = (int)k2.GetValue("9"); text[9] = (int)k2.GetValue("10"); text[10] = (int)k2.GetValue("11"); text[11] = (int)k2.GetValue("12"); text[12] = (int)k2.GetValue("13"); text[13] = (int)k2.GetValue("14"); win = (int)k2.GetValue("15");
catch {
text[0] = 81;
text[1] = 50;
text[2] = 51;
text[3] = 52;
text[4] = 53;
text[5] = 54;
text[6] = 48;
text[7] = 48;
text[8] = 48;
text[9] = 48;
text[10] = 48;
text[11] = 48;
text[12] = 48;
text[13] = 48;
win = 1;
k2.SetV alue("1",text[0]); k2.SetV alue("2",text[1]); k2.SetV alue("3",text[2]); k2.SetV alue("4",text[3]); k2.SetV alue("5",text[4]); k2.SetV alue("6",text[5]); k2.SetV alue("7",text[6]); k2.SetV alue("8",text[7]); k2.SetV alue("9",text[8]); k2.SetV alue("10",text[9]); k2.SetV alue("11",text[10]); k2.SetV alue("12",text[11]); k2.SetV alue("13",text[12]); k2.SetV alue("14",text[13]); k2.SetV alue("15",win);
}
textBox1.Text = ((char)text[0]).ToString(); textBox2.Text = ((char)text[1]).ToString(); textBox3.Text = ((char)text[2]).ToString(); textBox4.Text = ((char)text[3]).ToString(); textBox5.Text = ((char)text[4]).ToString(); textBox6.Text = ((char)text[5]).ToString(); textBox7.Text = ((char)text[6]).ToString(); textBox8.Text = ((char)text[7]).ToString(); textBox9.Text = ((char)text[8]).ToString(); textBox10.Text = ((char)text[9]).ToString();
textBox11.Text= ((char)text[10]).ToString(); textBox12.Text = ((char)text[11]).ToString(); textBox13.Text = ((char)text[12]).ToString(); textBox14.Text = ((char)text[13]).ToString();
if (win == 1) checkBox1.Checked = true; else checkBox1.Checked = false;
Sethook(); }
void Sethook() {
hp = new HookProc(KbHookProc);
Process curProcess = Process.GetCurrentProcess();
ProcessModule curModule = curProcess.MainModule;
hHook = SetWindowsHookEx(13, hp,
GetModuleHandle(curModule.ModuleName), IntPtr.Zero); }
void UnHook() {
if (hHook != 0) {
UnhookWindowsHookEx(hHook);
hHook = 0; }
}
void press(byte vk) {
keybd_event(vk, 0, 0, 0);
keybd_event(vk, 0, 0x0002, 0); }
voidcheck() {
toolTip1.Show(null, label5); if (openhook == false)
{
openhook = true;
button1.Text = "关闭改键[Home]";
tool Ti p1.Show( "改键已开启" ,label5,500); System.Media.SystemSounds.Asteri sk.P l ay();
} else {
} }
openhook = false;
button1.Text = "打开改键[Home]";
tool Ti p1.Show( "改键已关闭" ,label5,500); System.Media.SystemSounds.Excl amation.Pl ay();
int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
if (Marshal.ReadInt32(lParam) == (int)Keys.Home && wParam == (IntPtr)0x100) {
check();
return 1; }
IntPtr hwnd = FindWindow(null,"WarcraftIII");
if (openhook && hwnd != IntPtr.Zero && nCode >= 0 && wParam == (IntPtr)0x100 )
{
if ( IsIconic(hwnd) == IntPtr.Zero) {
int vkCode = Marshal.ReadInt32(lParam);
if (vkCode == text[0]) { press((byte)Keys.NumPad7); return 1; } else if (vkCode == text[1]) { press((byte)Keys.NumPad8); return 1; } else if (vkCode == text[2]) { press((byte)Keys.NumPad4); return 1; } else if (vkCode == text[3]) { press((byte)Keys.NumPad5); return 1; } else if (vkCode == text[4]) { press((byte)Keys.NumPad1); return 1; } else if (vkCode == text[5]) { press((byte)Keys.NumPad2); return 1; } else if (vkCode == text[7]) { press((byte)text[6]); return 1; }
else if (vkCode == text[9]) { press((byte)text[8]); return 1; }
else if (vkCode == text[11]) { press((byte)text[10]); return 1; }
else if (vkCode == text[13]) { press((byte)text[12]); return 1; }
else if (vkCode == (int)Keys.LWin&& win== 1) return 1;
return 0; }
}
return 0; }
private void button1_Click(object sender, EventArgs e) {
check(); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
UnHook(); }
private void 还原 ToolStripMenuItem_Click(objectsender, EventArgs e) {
this.Visible=true;
this.WindowState = FormWindowState.Normal; }
private void 退出 ToolStripMenuItem_Click(objectsender, EventArgs e) {
UnHook();
Appli cation.E xit(); }
private void button2_Click(object sender, EventArgs e) {
this.Visible= false; }
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("1",e.KeyValue);
text[0] = e.KeyValue;
textBox1.Text = e.KeyData.ToString();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("2",e.KeyValue);
text[1] = e.KeyValue;
textBox2.Text = e.KeyData.ToString();
}
private void textBox3_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("3",e.KeyValue);
text[2] = e.KeyValue;
textBox3.Text = e.KeyData.ToString();
}
private void textBox4_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("4",e.KeyValue);
text[3] = e.KeyValue;
textBox4.Text = e.KeyData.ToString(); }
private void textBox5_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("5",e.KeyValue);
text[4] = e.KeyValue;
textBox5.Text = e.KeyData.ToString();
}
private void textBox6_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("6",e.KeyValue);
text[5] = e.KeyValue;
textBox6.Text = e.KeyData.ToString();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
this.Visible=true;
this.WindowState = FormWindowState.Normal; }
private void textBox7_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("7",e.KeyValue);
text[6] = e.KeyValue;
textBox7.Text = e.KeyData.ToString();
}
private void textBox8_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("8",e.KeyValue);
text[7] = e.KeyValue;
textBox8.Text = e.KeyData.ToString();
}
private void textBox9_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("9",e.KeyValue);
text[8] = e.KeyValue;
textBox9.Text = e.KeyData.ToString();
}
private void textBox10_KeyDown(object sender, KeyEventArgs e)
{
k2.SetV alue("10",e.KeyValue);
text[9] = e.KeyValue;
textBox10.Text = e.KeyData.ToString();
}
private void textBox11_KeyDown(objectsender, KeyEventArgs e) {
k2.SetV alue("11",e.KeyValue); text[10] = e.KeyValue; textBox11.Text= e.KeyData.ToString();
}
private void textBox12_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("12",e.KeyValue); text[11] = e.KeyValue;
textBox12.Text = e.KeyData.ToString();
}
private void textBox13_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("13",e.KeyValue); text[12] = e.KeyValue;
textBox13.Text = e.KeyData.ToString();
}
private void textBox14_KeyDown(object sender, KeyEventArgs e) {
k2.SetV alue("14",e.KeyValue); text[13] = e.KeyValue;
textBox14.Text = e.KeyData.ToString();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
if (checkBox1.Checked == true) win= 1; else win = 0;
k2.SetV alue("15",win);
}
private void groupBox1_MouseHover(object sender, EventArgs e) {
toolTip1.Show("依次对应物品栏", label5);
} }
}
}
private void groupBox3_MouseHover(object sender, EventArgs e) {
toolTip1.Show("左边键改为右边键", label5); }
private void button1_MouseHover(object sender, EventArgs e) {
toolTip1.Show("打开/关闭改键", label5); }
private void button2_MouseHover(object sender, EventArgs e) {
toolTip1.Show("隐藏到状态栏", label5); }
private void checkBox1_MouseHover(object sender, EventArgs e) {
toolTip1.Show("左 Win 键是否作用", label5); }
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
this.Visible=true;
this.WindowState = FormWindowState.Normal; }
觉得不错,点个赞吧
原文地址:https://www.cnblogs.com/luangeng/p/5715950.html