0

我正在尝试为 Num Lock、Caps Lock 和 Scroll Lock 切换键盘上的指示灯。(我只是想让它们在夜间自动关闭。)使用 AutoHotkey 或 AutoIt 很简单。但是,如果工作站被锁定,该脚本将不起作用。

在研究这样做会调用某种 DLL 时,我遇到SetKeyboardStateuser32.dll. 不幸的是,根据 Windows 开发中心文档,无法使用SetKeyboardState.

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setkeyboardstate


另一个线程说只是ControlSend在 AutoIt 中使用。这通常非常可靠,即使没有活动的 GUI 会话,或者交互使用键盘/鼠标。

以下效果很好:

ControlSend("", "", "", "{NUMLOCK off}")

...但仅当存在活动的 GUI 会话时。这在屏幕锁定时无效。


我知道这些是特殊键:它们不是控制软件线程的输入状态,而是物理键盘状态的一部分,并控制整个系统的全局输入。

有什么选择吗?任何语言?

4

1 回答 1

0

至少在 Windows 上,当工作站被锁定时,似乎真的无法切换这些键。无论使用什么语言或框架,都需要通过底层的OS层。如果没有交互式会话,则不会发送这些按键。

自然,如果您站在键盘前按下这些键,它们的反应就会很好。

因此,这可以通过控制键盘作为附加外围设备来完成,例如 Arduino。其中一些型号可以用作 USB 键盘/鼠标。(我已经使用 Arduino Leonardo 和 Spark Fun Pro Micro 进行了尝试。对于这个用例,两者的响应方式相同。)

注意:即使 Arduino 或终端上的人员更新了切换键,操作系统中的切换键状态也不会更新,直到工作站解锁。无论工作站锁定时切换键处于何种状态,无论锁定终端上的人使用键盘做什么,该键都将继续以该状态出现在任何正在运行的脚本中,直到工作站解锁。这可以使用下面的 AHK 脚本轻松验证。


下面是 AutoHotKey 控制脚本的最小示例(尽管任何可以通过串行连接发送数据的程序都可以)和 Arduino 草图:

AutoHotKey 控制脚本

Loop
{
  RS232_FileHandle := RS232_Initialize()
  if (RS232_FileHandle)
  {
    ; Turn them all off
    (1 = GetKeyState("NumLock", "T")) ? RS232_Write(RS232_FileHandle, "219") : NA
    Sleep, 750
    (1 = GetKeyState("CapsLock", "T")) ? RS232_Write(RS232_FileHandle, "193") : NA
    Sleep, 750
    (1 = GetKeyState("ScrollLock", "T")) ? RS232_Write(RS232_FileHandle, "207") : NA

    Sleep, 4000

    ; Turn them all on
    (0 = GetKeyState("NumLock", "T")) ? RS232_Write(RS232_FileHandle, "219") : NA
    Sleep, 750
    (0 = GetKeyState("CapsLock", "T")) ? RS232_Write(RS232_FileHandle, "193") : NA
    Sleep, 750
    (0 = GetKeyState("ScrollLock", "T")) ? RS232_Write(RS232_FileHandle, "207") : NA

    RS232_Close(RS232_FileHandle)
  }
  Sleep, 4000
}

RS232_LoadSettings()
{
  RS232_Port     := "COM3"
  RS232_Baud     := "9600"
  RS232_Parity   := "N"
  RS232_DataBits := "8"
  RS232_StopBits := "1"
  RS232_Timeout  := "Off"
  RS232_XonXoff  := "Off"
  RS232_CTS_Hand := "Off"
  RS232_DSR_Hand := "Off"
  RS232_DSR_Sens := "Off"
  RS232_DTR      := "Off"
  RS232_RTS      := "Off"

  ; MSDN Reference: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/mode
  RS232_Settings = %RS232_Port%:BAUD=%RS232_Baud% PARITY=%RS232_Parity% DATA=%RS232_DataBits% STOP=%RS232_StopBits% to=%RS232_Timeout% xon=%RS232_XonXoff% odsr=%RS232_DSR_Hand% octs=%RS232_CTS_Hand% dtr=%RS232_DTR% rts=%RS232_RTS% idsr=%RS232_DSR_Sens%
  return RS232_Settings
}

RS232_Initialize()
{
  ; Source adapted from: https://autohotkey.com/board/topic/26231-serial-com-port-console-script/
  RS232_Settings := RS232_LoadSettings()
  RS232_Port     := StrSplit(RS232_Settings, ":")[1]
  RS232_COM      := (4 <= StrLen(RS232_Port) ? "\\.\" : "") . RS232_Port
  StringTrimLeft, RS232_Settings, RS232_Settings, StrLen(RS232_Port)+1
  VarSetCapacity(DCB, 28)
  if (1 <> DllCall("BuildCommDCB","str",RS232_Settings,"UInt",&DCB))
  {
    return false
  }
  hCom := DllCall("CreateFile","Str",RS232_COM,"UInt",0xC0000000,"UInt",3,"UInt",0,"UInt",3,"UInt",0,"UInt",0,"Cdecl Int")
  if (hCom < 1)
  {
    return false
  }
  if (1 <> DllCall("SetCommState","UInt",hCom,"UInt",&DCB))
  {
    RS232_Close(hCom)
    return false
  }
  VarSetCapacity(Data, 20, 0)
  NumPut(0xffffffff, Data,  0, "UInt")
  NumPut(0x00000000, Data,  4, "UInt")
  NumPut(0x00000000, Data,  8, "UInt")
  NumPut(0x00000000, Data, 12, "UInt")
  NumPut(0x00000000, Data, 16, "UInt")
  if (1 <> DllCall("SetCommTimeouts","UInt",hCom,"UInt",&Data))
  {
    RS232_Close(hCom)
    return false
  }
  return hCom
}

RS232_Write(hCom, msg)
{
  SetFormat, Integer, DEC
  StringSplit, Byte, msg, `,
  Data_Length := Byte0
  VarSetCapacity(Data, Byte0, 0xFF)
  i := 1
  Loop %Byte0%
  {
    NumPut(Byte%i%, Data, (i-1) , "UChar")
    i++
  }

  Bytes_Sent := 0
  WF_Result := DllCall("WriteFile","UInt",hCom,"UInt",&Data,"UInt",Data_Length,"UInt*",Bytes_Sent,"Int","NULL")
  if (WF_Result <> 1 or Bytes_Sent <> Data_Length)
  {
    return false
  }
  return Bytes_Sent
}

RS232_Close(hCom)
{
  return (1 == DllCall("CloseHandle","UInt",hCom))
}

阿杜诺素描

/* Pro Micro NumCapsScrollToggleDemo
   by: Jonathan David Arndt
   date: March 6, 2020

   This will allow the toggle of the Num Lock, Caps Lock, and Scroll Lock keys
   on the keyboard, via commands sent over USB serial
*/

#include <Keyboard.h>

// You could patch this into your Keyboard.h file, or just define it here
// Source: https://forum.arduino.cc/index.php?topic=173583.0 (attachment: USBAPI.h)
#define KEY_NUM_LOCK    0xDB
#define KEY_SCROLL_LOCK 0xCF

void pressAndRelease(int c);

void setup()
{
  Serial.begin(9600); // This pipes to the serial monitor
  delay(3000);        // Wait a moment for things to get setup
  Serial.println("Initialize Serial Monitor");
}

void loop()
{
  int c = 0;

  if (0 < Serial.available())
  {
    c = Serial.read();
    if (219 == c)
    {
      pressAndRelease(KEY_NUM_LOCK);
    }
    else if (193 == c)
    {
      pressAndRelease(KEY_CAPS_LOCK);
    }
    else if (207 == c)
    {
      pressAndRelease(KEY_SCROLL_LOCK);
    }
  }
}

void pressAndRelease(int c)
{
  Keyboard.press(c);
  Keyboard.release(c);
}
于 2020-03-12T02:19:07.760 回答