简体   繁体   English

C#在所有窗口中检测关键事件

[英]C# detecting key event in all windows

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. 现在,仅当我在窗体中按F2时才绘制正方形,但我希望它在所有窗口上都能工作。 What should i need more? 我还需要什么? (this is kinda auto drawer for perfect shapes) (这是一种用于完美形状的自动抽屉)

If you only want to handle a couple key combinations, you can use RegisterHotKey . 如果只想处理几个键组合,则可以使用RegisterHotKey If you want to detect all different key events, go with the global hook as Paul suggested . 如果您想检测所有不同的按键事件,请按照Paul的建议使用全局钩子

You need a global hook. 您需要一个全局钩子。 There's a brilliant implementation here http://www.codeproject.com/KB/cs/globalhook.aspx http://www.codeproject.com/KB/cs/globalhook.aspx这里有一个出色的实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM