简体   繁体   中英

Why won't my Input Hook work?

I'm trying to make an application that can log user inactivity through an input hook, however I'm stuck at the moment because the compiler throws an error saying

an object reference is required for the non-static field, method, or property 'NotifyIcon.InputHook.MouseHookProcedure'

This is my inactivity custom event handler code

public void inactivity_Active(object sender, EventArgs e)
        {

            if (InputHook.hHook == 0)
            {

               InputHook.MouseHookProcedure = new InputHook.HookProc(InputHook.MouseHookProc);

              InputHook.hHook = InputHook.SetWindowsHookEx(InputHook.WH_MOUSE,
                  InputHook.MouseHookProcedure,
                   (IntPtr)0,
                   AppDomain.GetCurrentThreadId());

            }

and for my InputHook Class, this sets up the Input Hook

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;




namespace NotifyIcon
{
    public class InputHook
    {
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        public static int hHook = 0;

        public const int WH_MOUSE = 7;

        public HookProc MouseHookProcedure;

        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }


        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
        IntPtr wParam, IntPtr lParam);


        public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //Marshall the data from the callback.
            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

            if (nCode < 0)
            {
                return CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                //Create a string variable that shows the current mouse coordinates.
                String strCaption = "x = " +
                        MyMouseHookStruct.pt.x.ToString("d") +
                            "  y = " +
                MyMouseHookStruct.pt.y.ToString("d");
                return CallNextHookEx(hHook, nCode, wParam, lParam); 


            }
        }
    }
}

Any and all help with this issue would be greatly appreciated =]

您需要将其设为静态,因为它不是实例属性

public static HookProc MouseHookProcedure;

使其静态:

public static HookProc MouseHookProcedure;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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