public void KeyUp(object sender, KeyEventArgs e) { string str = ""; #region ShiftKey, LControlKey, LMenu, LWin와 같은 특수키가 눌렸을 때의 처리 switch (e.KeyCode) { case Keys.LShiftKey: str = "LShiftKey Up"; break; case Keys.RShiftKey: str = "RShiftKey Up"; break; case Keys.LControlKey: str = "LControlKey Up"; break; case Keys.LMenu: str = "LMenu Up"; break; case Keys.LWin: str = "LWin Up"; break; case Keys.NumLock: ImportFunctions.keybd_event((byte)Keys.NumLock, 0, 0x02, 0); break; case Keys.Capital: ImportFunctions.keybd_event((byte)Keys.Capital, 0, 0x02, 0); break; } #endregion if (str != "") { dt_class.Rec_list.Add(str); // 레코딩 List 이벤트 기록 MainRecording.Instance.LogXamlList.Items.Add("Keyboard Count " + dt_class.Rec_list.Count().ToString() + " : " + str); MainRecording.Instance.LogXamlList.ScrollIntoView(MainRecording.Instance.LogXamlList.Items[MainRecording.Instance.LogXamlList.Items.Count - 1]); // 레코딩 ShowList 이벤트 기록 MainRecording.Instance.ShowList_Dlg.S_List.Items.Add("Keyboard Count " + dt_class.Rec_list.Count().ToString() + " : " + str); MainRecording.Instance.ShowList_Dlg.S_List.ScrollIntoView(MainRecording.Instance.ShowList_Dlg.S_List.Items[MainRecording.Instance.ShowList_Dlg.S_List.Items.Count - 1]); } return; }
public void GetWindowName() { const int nChars = 256; IntPtr handle; StringBuilder Buff = new StringBuilder(nChars); handle = (IntPtr)ImportFunctions.GetForegroundWindow(); if (ImportFunctions.GetWindowText((int)handle, Buff, nChars) > 0) { Content_textBox.Text = Buff.ToString(); listView.Items.Add(new InitMemo(List_textBox.Text, Content_textBox.Text)); listView.SelectedIndex = listView.Items.Count - 1; List_textBox.Text = ""; Content_textBox.Text = ""; } }
private static void ForceUnsunscribeFromGlobalMouseEvents() { if (s_MouseHookHandle != 0) { //uninstall hook int result = ImportFunctions.UnhookWindowsHookEx(s_MouseHookHandle); //reset invalid handle s_MouseHookHandle = 0; //Free up for GC s_MouseDelegate = null; //if failed and exception must be thrown if (result == 0) { //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. int errorCode = Marshal.GetLastWin32Error(); //Initializes and throws a new instance of the Win32Exception class with the specified error. throw new Win32Exception(errorCode); } } }
private static void EnsureSubscribedToGlobalMouseEvents() { // install Mouse hook only if it is not installed and must be installed // 필요한 경우에만 마우스 훅을 설치 if (s_MouseHookHandle == 0) { //See comment of this field. To avoid GC to clean it up. s_MouseDelegate = MouseHookProc; // 마우스훅 proc에서 받아온 값을 대리자에 전달하고 //install hook //s_MouseHookHandle = SetWindowsHookEx( // WH_MOUSE_LL, // s_MouseDelegate, // Marshal.GetHINSTANCE( // Assembly.GetExecutingAssembly().GetModules()[0]), // 0); var mar = ImportFunctions.LoadLibrary("user32.dll"); //FrameWork 4.0 and up, Using this is alternative s_MouseHookHandle = ImportFunctions.SetWindowsHookEx( Constants.WH_MOUSE_LL, s_MouseDelegate, mar, 0); //If SetWindowsHookEx fails. if (s_MouseHookHandle == 0) { //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. int errorCode = Marshal.GetLastWin32Error(); //do cleanup //Initializes and throws a new instance of the Win32Exception class with the specified error. throw new Win32Exception(errorCode); } } }
public void LShiftKeyUp() // LControl이 눌렸을 경우 UpEvent를 호출 { Keys cKey = Keys.LShiftKey; ImportFunctions.keybd_event((byte)cKey, 0, 0x02, 0); }
/// <summary> /// A callback function which will be called every Time a mouse activity detected. /// </summary> /// <param name="nCode"> /// [in] Specifies whether the hook procedure must process the message. /// If nCode is HC_ACTION, the hook procedure must process the message. /// If nCode is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the /// value returned by CallNextHookEx. /// </param> /// <param name="wParam"> /// [in] Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="lParam"> /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private static int MouseHookProc(int nCode, int wParam, IntPtr lParam) // wparam lparam ncode받아서 대리자 객체에 넘겨줌 { if (nCode >= 0) { //Marshall the data from callback. Structs.MouseLLHookStruct mouseHookStruct = (Structs.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(Structs.MouseLLHookStruct)); // 관련된 마우스 이벤트를 후킹해서 가져옴 //detect button clicked MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; bool mouseDown = false; bool mouseUp = false; switch (wParam) { case Constants.WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; case Constants.WM_LBUTTONUP: mouseUp = true; button = MouseButtons.Left; clickCount = 1; break; case Constants.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case Constants.WM_RBUTTONDOWN: mouseDown = true; button = MouseButtons.Right; clickCount = 1; break; case Constants.WM_RBUTTONUP: mouseUp = true; button = MouseButtons.Right; clickCount = 1; break; case Constants.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case Constants.WM_MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, MouseData is not used. break; } //generate event MouseEventExtArgs e = new MouseEventExtArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); //Mouse up if (s_MouseUp != null && mouseUp) { s_MouseUp.Invoke(null, e); // 아무것도 들어갈 필요가 없어서 null 호출만 시키면 되기 때문에 if (MouseUp_Block) { return(s_MouseHookHandle); } } //Mouse down if (s_MouseDown != null && mouseDown) // 마우스가 클릭된 상황에 MouseEventHandler의 대리자 객체로 선언한 s_MouseDown(마우스클릭성공)의 이벤트를 발생시키기 위해서 s_MouseDown.Invoke(null,e)를 선언 { s_MouseDown.Invoke(null, e); if (MouseDown_Block) { return(s_MouseHookHandle); } } //If someone listens to click and a click is heppened if (s_MouseClick != null && clickCount > 0) { s_MouseClick.Invoke(null, e); } //If someone listens to click and a click is heppened if (s_MouseClickExt != null && clickCount > 0) { s_MouseClickExt.Invoke(null, e); } //If someone listens to double click and a click is heppened if (s_MouseDoubleClick != null && clickCount == 2) { s_MouseDoubleClick.Invoke(null, e); } //Wheel was moved if (s_MouseWheel != null && mouseDelta != 0) { s_MouseWheel.Invoke(null, e); } //만약 누군가가 마우스를 이동시켜 좌표가 변경되게 되면 move이벤트를 발생시킨다. if ((s_MouseMove != null || s_MouseMoveExt != null) && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y)) { m_OldX = mouseHookStruct.Point.X; m_OldY = mouseHookStruct.Point.Y; if (s_MouseMove != null) { s_MouseMove.Invoke(null, e); } if (s_MouseMoveExt != null) { s_MouseMoveExt.Invoke(null, e); } } if (e.Handled) { return(-1); } } //call next hook return(ImportFunctions.CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam)); }
/// 키보드 입력이 일어났을 때 훅 프로시저 호출 public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) { try { if (code >= 0) { Keys key = (Keys)lParam.vkCode; if (key == Keys.None) { return(0); } if (HookedKeys.Contains(key)) { KeyEventArgs kea = new KeyEventArgs(key); if ((wParam == Constants.WM_KEYDOWN) && (KeyDown != null)) { KeyboardHooking.Instance.KeyDown(this, kea); } else if ((wParam == Constants.WM_KEYUP || wParam == Constants.WM_SYSKEYUP) && (KeyUp != null)) { if (KeyboardHooking.Instance.recording_starting && KeyboardHooking.Instance.is_startsetting) { KeyboardHooking.Instance.KeyUp(this, kea); } //KeyUp(this, kea); } else if (wParam == Constants.WM_SYSKEYDOWN) { #region 키보드로 컨트롤 하기 위한 제어문 if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.RecordStartKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordStartButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.LogPlayKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordPlayButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.LogPlayStopKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordPlayStopButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.RecordStopKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordStopButtonEvent(); return(1); } #endregion else if (KeyboardHooking.Instance.recording_starting && KeyboardHooking.Instance.is_startsetting) { KeyboardHooking.Instance.KeyDown(this, kea); } } else if (wParam == Constants.WM_SYSKEYUP) { } if (kea.Handled) { return(1); } } } return(ImportFunctions.CallNextHookEx(hhook, code, wParam, ref lParam)); } catch { return(0); } }
/// <summary> /// Uninstalls the global hook /// </summary> public void unhook() { ImportFunctions.UnhookWindowsHookEx(hhook); }
/// <summary> /// Installs the global hook /// </summary> public void hook() { IntPtr hInstance = ImportFunctions.LoadLibrary("User32"); hhook = ImportFunctions.SetWindowsHookEx(Constants.WH_KEYBOARD_LL, khp, hInstance, 0); }