按游戏中的关键,这将使我的程序点击“开始” [英] Pressing a Key in a game which will make my program click 'Start'
问题描述
我目前在做一个计划,将宣布和倒计时的例子可以说5分钟,所以当你按开始按钮,它就会从5分钟倒数至0,
I am currently doing a program which will Announce and Countdown from example lets say 5 minutes, So when you press Start button it will count down from 5 minutes to 0,
这是我想拥有的东西是这样的:
The thing that I would like to have is this:
如果我在一个全屏幕的游戏,例如魔兽世界,英雄联盟,或全屏幕图形的任何比赛,我按<大骨节病>小键盘8 骨节病>,我想程序点击开始按钮1
If I am in a full-screen game, for example World of Warcraft , League of Legends, or any game with full screen graphics and I press Numpad 8, I want the program to click start on Button 1.
因为我以前见过,但我不知道该怎么办呢?这是可能的。
This should be possible since I've seen it before but I don't know how to do it.
推荐答案
我测试了它的传奇和界面完全音频联盟。以下工作:
I tested it on League of Legends and Terraria. The following works:
public static class WindowsAPI
{
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyboardHookStruct
{
public int VirtualKeyCode;
public int ScanCode;
public int Flags;
public int Time;
public int ExtraInfo;
}
public delegate IntPtr HookProc(int nCode, IntPtr wp, IntPtr lp);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(HookType idHook, HookProc lpfn, IntPtr hInstance, uint threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr hHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CallNextHookEx(HookType idHook, int nCode, IntPtr wParam, IntPtr lParam);
}
再到钩子起来:
Then to "hook" it up:
public partial class Form1 : Form
{
private IntPtr kbhook = IntPtr.Zero;
private void Form1_Load(object sender, EventArgs e)
{
kbhook = WindowsAPI.SetWindowsHookEx(WindowsAPI.HookType.WH_KEYBOARD_LL, HandleKeyPress, IntPtr.Zero, 0);
if (kbhook == IntPtr.Zero)
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
WindowsAPI.UnhookWindowsHookEx(kbhook);
}
private IntPtr HandleKeyPress(int nCode, IntPtr wp, IntPtr lp)
{
WindowsAPI.KeyboardHookStruct MyKeyboardHookStruct =
(WindowsAPI.KeyboardHookStruct)Marshal.PtrToStructure(lp, typeof(WindowsAPI.KeyboardHookStruct));
var key = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
// **********************************
// if the pressed key is Keys.NumPad8
if (key == Keys.NumPad8)
{
button1_Click(null, EventArgs.Empty);
}
return WindowsAPI.CallNextHookEx(WindowsAPI.HookType.WH_KEYBOARD_LL, nCode, wp, lp);
}
}
的 pinvoke.net 可能有过时的代码样本,但它有助于了解该方法签名。
pinvoke.net may have outdated code samples, but it helps to know the method signatures.
这篇关于按游戏中的关键,这将使我的程序点击“开始”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!