C# 检测所有窗口中的按键事件

发布于 2024-10-26 10:22:49 字数 2400 浏览 2 评论 0原文

嘿,我对关键事件处理程序有一些问题。这是来源:

using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Diagnostics;
        using System.Threading;

        namespace WindowsFormsApplication3
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }

                [System.Runtime.InteropServices.DllImport("user32.dll")]
                public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;


                public void Form1_KeyPessed(object sender, KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.F2)
                    {
                        DrawSquare();
                    }
                }

                public void DrawSquare()
                {
                    int line = Convert.ToInt16(textBox1.Text);
                    int time = Convert.ToInt16(textBox2.Text);

                    int x = MousePosition.X;
                    int y = MousePosition.Y;
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x + line / 2, y - line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x, y);
                }
            }      
        }

现在,当我在表单中按 F2 时,它只会绘制正方形,但我希望它适用于所有窗口。 我还需要什么? (这是一种完美形状的自动抽屉)

Hey, i have some problem with key event handler. This is source:

using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Diagnostics;
        using System.Threading;

        namespace WindowsFormsApplication3
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }

                [System.Runtime.InteropServices.DllImport("user32.dll")]
                public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;


                public void Form1_KeyPessed(object sender, KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.F2)
                    {
                        DrawSquare();
                    }
                }

                public void DrawSquare()
                {
                    int line = Convert.ToInt16(textBox1.Text);
                    int time = Convert.ToInt16(textBox2.Text);

                    int x = MousePosition.X;
                    int y = MousePosition.Y;
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x + line / 2, y - line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x, y);
                }
            }      
        }

Now it only draws square when i press F2 in form, but i want it to work on all windows.
What should i need more?
(this is kinda auto drawer for perfect shapes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等数载,海棠开 2024-11-02 10:22:49

如果您只想处理几个组合键,可以使用 RegisterHotKey。如果您想检测所有不同的按键事件,请使用 全局按照保罗的建议进行钩子

If you only want to handle a couple key combinations, you can use RegisterHotKey. If you want to detect all different key events, go with the global hook as Paul suggested.

如果没结果 2024-11-02 10:22:49

你需要一个全局钩子。这里有一个出色的实现 http://www.codeproject.com/KB/cs/globalhook。 ASPX

You need a global hook. There's a brilliant implementation here http://www.codeproject.com/KB/cs/globalhook.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文