简体   繁体   English

如何使用C#全局键盘挂钩事件获取当前的键盘光标点

[英]How to get current keyboard cursor point using c# global keyboard hook event

Im working with ac# application which captures the system events using the global c# mouse and keyboard hooks, But I couldn't get the current keyboard cursor position values while im typing. 我正在使用ac#应用程序,该应用程序使用全局c#鼠标和键盘挂钩捕获系统事件,但是在im键入时我无法获取当前的键盘光标位置值。

Following is my code and it always returns GetCaretPos output as (0,0) 以下是我的代码,它始终以(0,0)返回GetCaretPos输出

PointDetail curPoint = new PointDetail();
Point position = new Point();
IntPtr hwndFoc;
IntPtr hwndFG = WinApiDelegate.GetForegroundWindow();
uint processID = 0;
uint mainWindowProcessId = 0;
IntPtr activeWindowThreadProcess = WinApiDelegate.GetWindowThreadProcessId(hwndFG, IntPtr.Zero);
IntPtr currWindowThread = IntPtr.Zero;
int thisWindowThread = 0;

this.Invoke(new MethodInvoker(delegate
    {
        currWindowThread = WinApiDelegate.GetWindowThreadProcessId(this.Handle, IntPtr.Zero);
        thisWindowThread = WinApiDelegate.GetWindowThreadProcessId(this.Handle, out mainWindowProcessId);
    }));
int activeWindowThread = WinApiDelegate.GetWindowThreadProcessId(hwndFG, out processID);

if (activeWindowThread != Thread.CurrentThread.ManagedThreadId)
{
    WinApiDelegate.AttachThreadInput(activeWindowThreadProcess, currWindowThread, true);
    hwndFoc = WinApiDelegate.GetActiveWindow();
    bool CaretPos = WinApiDelegate.GetCaretPos(ref position);
}

You can find the cursor position using the caretPosition. 您可以使用caretPosition查找光标位置。 Try the below code. 试试下面的代码。

[DllImport("user32.dll", EntryPoint = "GetGUIThreadInfo")]
public static extern bool GetGUIThreadInfo(uint tId, out GUITHREADINFO threadInfo);

        [StructLayout(LayoutKind.Sequential)]    // Required by user32.dll
        public struct RECT
        {
            public uint Left;
            public uint Top;
            public uint Right;
            public uint Bottom;
        };

        [StructLayout(LayoutKind.Sequential)]    // Required by user32.dll
        public struct GUITHREADINFO
        {
            public uint cbSize;
            public uint flags;
            public IntPtr hwndActive;
            public IntPtr hwndFocus;
            public IntPtr hwndCapture;
            public IntPtr hwndMenuOwner;
            public IntPtr hwndMoveSize;
            public IntPtr hwndCaret;
            public RECT rcCaret;
        }; 

private System.Windows.Point EvaluateCaretPosition()
        {
            caretPosition = new Point();
            try
            {
                // Fetch GUITHREADINFO
                GetCaretPosition();
                caretPosition.X = (int)guiInfo.rcCaret.Left; //+ 25;
                caretPosition.Y = (int)guiInfo.rcCaret.Bottom; //+ 25;
                WinApiDelegate.ClientToScreen(guiInfo.hwndCaret, ref caretPosition);
            }
            catch (Exception Ex)
            {
                GenerateConsolidatedErrorLog(Ex);
            }
            return new System.Windows.Point(caretPosition.X, caretPosition.Y);
        } 
       public void GetCaretPosition()
        {
            try
            {
                guiInfo = new WinApiDelegate.GUITHREADINFO();
                guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo);
                // Get GuiThreadInfo into guiInfo
                WinApiDelegate.GetGUIThreadInfo(0, out guiInfo);
            }
            catch (Exception Ex)
            {
                GenerateConsolidatedErrorLog(Ex);
            }
        } 

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

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