← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #285483]: isLockOn does not change until typing from the keyboard

 

Question #285483 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/285483

RaiMan posted a new comment:
@Alex
I am sorry for the inconvenience, but I cannot test this on a Windows 7 currently.
I developed and tested the implementation on Win10, where it works without problems.

This is the implementation according to the Windows API, where it
states, that this is valid since Win2000.

Env.isLock... is routed to here in class Key

  /**
   * get the lock state of the given key
   *
   * @param key as Character (scroll, caps, num)
   * @return true/false
   */
  public static boolean isLockOn(char key) {
//    Toolkit tk = Toolkit.getDefaultToolkit();
//        return tk.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);
//        return tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
//        return tk.getLockingKeyState(KeyEvent.VK_NUM_LOCK);
    if (!RunTime.get().runningWindows) {
      return false;
    }
    switch (key) {
      case '\ue025':
        return SysJNA.WinUser32.isScrollLockOn();
      case '\ue027':
        return SysJNA.WinUser32.isCapsLockOn();
      case '\ue03B':
        return SysJNA.WinUser32.isNumLockOn();
      default:
        return false;
    }
  }

... and the low level implementation in SysJNA.WinUser32:

  /**
   * Direct access to Windows API user32.dll via BridJ
   */
  @Library("user32")
  public static class WinUser32 {

    /*
    https://msdn.microsoft.com/pt-br/library/windows/desktop/dd375731
    VK_NUM_LOCK 0x90
    VK_SCROLL 0x91
    VK_CAPITAL 0x14
    */
    private static int WinNumLock = 0x90;
    private static int WinScrollLock = 0x91;
    private static int WinCapsLock = 0x14;

    static {
      BridJ.register();
    }

    public static boolean isNumLockOn() {
      int state = GetKeyState(WinNumLock);
      return state > 0;
    }

    public static boolean isScrollLockOn() {
      int state = GetKeyState(WinScrollLock);
      return state > 0;
    }

    public static boolean isCapsLockOn() {
      int state = GetKeyState(WinCapsLock);
      return state > 0;
    }

    /*
    https://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx
    SHORT WINAPI GetKeyState(
      _In_ int nVirtKey
    );
     */
    private static native int GetKeyState(int aVK);
  }

You might check the MS links and look for any possible reason.

I will only have a chance to go deeper after end of November (some
vacation ;-)

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.