aa18229927211
aa18229927211
2017-02-08 02:19
采纳率: 0%
浏览 3.3k

Java swing 实现对电脑屏幕的鼠标移动事件和键盘输入的监控

通俗点就是一个程序,能够对电脑屏幕上鼠标移动的事件和键盘输入的ascii值进行监控,本人一开始写了一个,但是只是在我的程序cs端内的范围有效,如何实现对整个屏幕的监控,有大神知道吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy

    用键盘钩子实现,java不方便写原生代码,可以用c++写好,记录下来,传给java的程序。
    http://download.csdn.net/download/Yilefeng/1923456
    http://blog.csdn.net/mrxiao23/article/details/50971579

    点赞 评论
  • lhtlmx
    lhtlmx 2017-02-08 04:07

    用JAVA确实不方便,用C/C++,VB的比较多,关于HOOK及监控,可以参考以下内容,希望对你有帮助

    http://www.koovin.com/?q=hook+%E9%94%AE%E7%9B%98%E7%9B%91%E6%8E%A7

    点赞 评论
  • u011606457
    _1_1_7_ 2017-02-09 03:22

    java有两种方案(我自己做过的)
    1.jna,代码结构类似:

        private final LowLevelKeyboardProc keyboardHook = new LowLevelKeyboardProc() {
    
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx
            public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
                int w = wParam.intValue();
                // 按下alt键时w=.WM_SYSKEYDOWN; 按下其他大部分键时w=WinUser.WM_KEYDOWN
                if (w == WinUser.WM_KEYDOWN || w == WinUser.WM_SYSKEYDOWN) {
                    for (KeyboardListener l : keyListeners) {
                        l.keyPressed(lParam.vkCode);
                    }
                } else if (w == WinUser.WM_KEYUP || w == WinUser.WM_SYSKEYUP) {
                    for (KeyboardListener l : keyListeners) {
                        l.keyReleased(lParam.vkCode);
                    }
                }
    
                return User32.INSTANCE.CallNextHookEx(keyboardHHK, nCode, wParam, lParam.getPointer());
            }
        };
    
        private final LowLevelMouseProc mouseHook = new LowLevelMouseProc() {
    
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644986(v=vs.85).aspx
            public LRESULT callback(int nCode, WPARAM wParam, MouseHookStructure lParam) {
                int x = lParam.pt.x;
                int y = lParam.pt.y;
                switch (wParam.intValue()) {
                    case WM_MOUSEMOVE :
                        for (MouseListener l : mouseListeners) {
                            l.mouseMoved(x, y);
                        }
                        break;
                    case WM_LBUTTONDOWN :
                        for (MouseListener l : mouseListeners) {
                            l.leftButtonDown(x, y);
                        }
                        break;
                    case WM_LBUTTONUP :
                        for (MouseListener l : mouseListeners) {
                            l.leftButtonUp(x, y);
                        }
                        break;
                    case WM_RBUTTONUP :
                        for (MouseListener l : mouseListeners) {
                            l.rightButtonUp(x, y);
                        }
                        break;
                    case WM_RBUTTONDOWN :
                        for (MouseListener l : mouseListeners) {
                            l.rightButtonDown(x, y);
                        }
                        break;
                    case WM_MOUSEWHEEL :
                        for (MouseListener l : mouseListeners) {
                            l.mouseWheelMoved();
                        }
                        break;
                }
                return User32.INSTANCE.CallNextHookEx(mouseHHK, nCode, wParam, lParam.getPointer());
            }
        };
    

    用的其实就是hook+回调函数

    2.org.sf.feeling.swt
    原理同上
    看官方的sample就行了

    点赞 评论