Example #1
0
  public void run() {
    lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook =
        new LowLevelKeyboardProc() {
          public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

            if (nCode >= 0) {
              // To unhook press 'esc' key
              if (info.vkCode == 0x1B) {
                User32.INSTANCE.UnhookWindowsHookEx(hhk);
              }
              switch (info.vkCode) {
                case 0x5B:
                  System.err.println("l win");
                  return new LRESULT(1);
                case 0x5C:
                  System.err.println("r win");
                  return new LRESULT(1);
                case 0xA2:
                  System.err.println("l ctrl");
                  return new LRESULT(1);
                case 0xA3:
                  System.err.println("r ctrl");
                  return new LRESULT(1);
                case 0xA4:
                  System.err.println("l alt");
                  return new LRESULT(1);
                case 0xA5:
                  System.err.println("r alt");
                  return new LRESULT(1);
                default:
                  System.out.println("Key Pressed : " + info.vkCode); // do nothing
              }
            }
            return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
          }
        };
    hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0);
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();

    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
      if (result == -1) {
        break;
      } else {
        lib.TranslateMessage(msg);
        lib.DispatchMessage(msg);
      }
    }

    lib.UnhookWindowsHookEx(hhk);
  }
Example #2
0
  public String GetWindowInfoXml() {

    final List<WindowInfo> infoList = new ArrayList<WindowInfo>();

    class ChildWindowCallback implements WinUser.WNDENUMPROC {
      public boolean callback(HWND hWnd, Pointer lParam) {
        infoList.add(new WindowInfo(hWnd, true));
        return true;
      }
    }
    class ParentWindowCallback implements WinUser.WNDENUMPROC {
      public boolean callback(HWND hWnd, Pointer lParam) {
        infoList.add(new WindowInfo(hWnd, false));
        user32.EnumChildWindows(hWnd, new ChildWindowCallback(), new Pointer(0));
        return true;
      }
    }
    user32.EnumWindows(new ParentWindowCallback(), 0);

    // convert list to string xml
    StringBuilder sb = new StringBuilder("");
    for (WindowInfo w : infoList) {
      // System.out.println(w);
      sb.append(w + "\r\n");
    }
    sb.append("count - " + infoList.size() + "\r\n");
    return sb.toString();
  }
Example #3
0
 public synchronized void release() {
   Logger.debug("release()");
   HHOOK hook = getHook();
   if (hook != null) {
     USER32_INSTANCE.UnhookWindowsHookEx(hHook);
     hook = null;
     // TODO ordinarily I'd interrupt the thread to force it to exit if it's
     // blocked, but in this case a fatal VM failure would occur
     // hookThread.interrupt();
   }
   Logger.debug("released");
 }
Example #4
0
  @Override
  public LRESULT callback(int nCode, WPARAM wParam, MSLLHOOKSTRUCT lParam) {
    Logger.trace("callback(nCode={},wParam={},lParam={})", nCode, wParam, lParam);
    if (nCode >= 0) {
      Window window = SwingUtilities.getWindowAncestor(relativeTo);
      Logger.trace("window={}", window);
      // Is the window active...
      if (window != null && window.isActive()) {
        Logger.trace("window is active");
        // Is the component showing...
        // TODO is this still needed or is isActive good enough?
        if (relativeTo.isShowing() && relativeTo.isValid()) {
          Logger.trace("window is visible");
          // Did the event occur inside the component bounds...
          int absX = lParam.pt.x;
          int absY = lParam.pt.y;
          // FIXME there is a race here where relativeTo may no longer be visible, should
          // I lock the component tree - is that OK from non-EDT?
          Point componentPoint = relativeTo.getLocationOnScreen();
          int relX = componentPoint.x;
          int relY = componentPoint.y;
          int relW = relX + relativeTo.getWidth();
          int relH = relY + relativeTo.getHeight();
          if (absX >= relX && absY >= relY && absX < relW && absY < relH) {
            Logger.trace("event inside component bounds");
            if (!this.mouseEntered) {
              this.mouseEntered = true;
              fireMouseEvent(MouseEvent.MOUSE_ENTERED, MouseEvent.NOBUTTON, lParam);
            }
            // The event did occur inside the component bounds, so translate it...
            switch (wParam.intValue()) {
              case WM_MOUSEMOVE:
                fireMouseMotionEvent(MouseEvent.MOUSE_MOVED, MouseEvent.NOBUTTON, lParam);
                break;

              case WM_LBUTTONDOWN:
                fireMouseEvent(MouseEvent.MOUSE_PRESSED, MouseEvent.BUTTON1, lParam);
                break;

              case WM_LBUTTONUP:
                fireMouseEvent(MouseEvent.MOUSE_RELEASED, MouseEvent.BUTTON1, lParam);
                break;

              case WM_RBUTTONDOWN:
                fireMouseEvent(MouseEvent.MOUSE_PRESSED, MouseEvent.BUTTON2, lParam);
                break;

              case WM_RBUTTONUP:
                fireMouseEvent(MouseEvent.MOUSE_RELEASED, MouseEvent.BUTTON2, lParam);
                break;

              case WM_MBUTTONDOWN:
                fireMouseEvent(MouseEvent.MOUSE_PRESSED, MouseEvent.BUTTON3, lParam);
                break;

              case WM_MBUTTONUP:
                fireMouseEvent(MouseEvent.MOUSE_RELEASED, MouseEvent.BUTTON3, lParam);
                break;

              case WM_MOUSEWHEEL:
                fireMouseWheelEvent(MouseEvent.MOUSE_WHEEL, lParam);
                break;

              default:
                break;
            }
          } else {
            Logger.trace("event outside component bounds");

            if (this.mouseEntered) {
              this.mouseEntered = false;
              fireMouseEvent(MouseEvent.MOUSE_EXITED, MouseEvent.NOBUTTON, lParam);
            }
          }
        }
      }
    }
    return USER32_INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
  }
Example #5
0
 public boolean restoreWindow(HWND handle) {
   return user32.ShowWindow(handle, WinUser.SW_RESTORE);
 }
Example #6
0
 public boolean minimizeWindow(HWND handle) {
   return user32.ShowWindow(handle, WinUser.SW_MINIMIZE);
 }
Example #7
0
 public boolean showWindow(HWND handle) {
   return user32.ShowWindow(handle, WinUser.SW_SHOW);
 }
Example #8
0
 public boolean activateWindow(HWND handle) {
   boolean result = user32.SetForegroundWindow(handle);
   return result;
 }
Example #9
0
 public HWND findWindowByTitle(String title) {
   HWND handle = user32.FindWindow(null, title);
   return handle;
 }