/// <summary> /// hook 回调 /// </summary> /// <param name="ncode">ncode</param> /// <param name="wparam">事件类型</param> /// <param name="lparam">事件参数结构体</param> /// <returns></returns> private int HookCallback(int ncode, IntPtr wparam, IntPtr lparam) { if (ncode >= 0) { int wparam_int = (int)wparam; if (HookTypeInt == (int)HookType.WH_MOUSE_LL) { MyMouseStruct ms = (MyMouseStruct)Marshal.PtrToStructure(lparam, typeof(MyMouseStruct)); MouseLParamStruct = ms; if (!MouseEventTypeList.Contains(wparam_int)) { MouseEventTypeList.Add(wparam_int); } } else if (HookTypeInt == (int)HookType.WH_KEYBOARD_LL) { MyKeyStruct ks = (MyKeyStruct)Marshal.PtrToStructure(lparam, typeof(MyKeyStruct)); KeyLParamStruct = ks; if (!KeyEventTypeList.Contains(wparam_int)) { KeyEventTypeList.Add(wparam_int); } } } return(CallNextHookEx(HookIntptr, ncode, wparam, lparam));; }
/// <summary> /// 提取按键结构体的vkcode并把它转换为字符串 /// </summary> /// <param name="lparamStuct"></param> /// <returns></returns> public static string GetKeyboardDownCodeStr(MyKeyStruct lparamStuct) { byte[] by = new byte[1]; by[0] = (byte)(lparamStuct.vkcode); return(Convert.ToString(Encoding.UTF8.GetString(by))); }