English 中文(简体)
在一场游戏中宣传一个关键因素,使我的节目点击开起点
原标题:Pressing a Key in a game which will make my program click Start

我目前正在实施一个方案,从例让中宣布放弃和倒计为5分钟,因此,当你发布<>Start,按规定,它将从5分钟减少到0分钟。

The thing that I would like to have is this:

如果我参加了一场全方位的游戏,例如:战争手工艺世界、图象联盟,或带有全方位插图的游戏和我的新闻Numpad 8 <,我希望节目在Button 1上点首。

这是可能的,因为我以前曾看到过这一点,但我不知道如何这样做。

问题回答

我在图象和Terraria进行了测试。 以下著作:

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);
}

接着是“忽略”:

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 密码样本可能已经过时,但有助于了解方法签名。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签