windows 在 Python 中更改键盘锁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/854393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:28:07  来源:igfitidea点击:

Change keyboard locks in Python

pythonwindows

提问by Lucas Jones

Is there any way, in Python, to programmatically changethe CAPS LOCK/NUM LOCK/SCROLL LOCK states?

在 Python 中,有什么方法可以以编程方式更改CAPS LOCK/NUM LOCK/SCROLL LOCK 状态?

This isn't really a joke question - more like a real question for a joke program. I intend to use it for making the lights do funny things...

这不是一个真正的笑话问题 - 更像是一个真正的笑话程序问题。我打算用它来让灯光做一些有趣的事情......

采纳答案by sclarson

If you're using windows you can use SendKeysfor this I believe.

如果您使用的是 Windows ,我相信您可以为此使用SendKeys

http://www.rutherfurd.net/python/sendkeys

http://www.rutherfurd.net/python/sendkeys

import SendKeys

SendKeys.SendKeys("""
{CAPSLOCK}
{SCROLLOCK}
{NUMLOCK}
""")

回答by Benji York

On Linux here's a Python program to blink all the keyboard LEDs on and off:

在 Linux 上,这是一个 Python 程序,用于打开和关闭所有键盘 LED:

import fcntl
import os
import time

KDSETLED = 0x4B32
SCR_LED  = 0x01
NUM_LED  = 0x02
CAP_LED  = 0x04

console_fd = os.open('/dev/console', os.O_NOCTTY)

all_on = SCR_LED | NUM_LED | CAP_LED
all_off = 0

while 1:
    fcntl.ioctl(console_fd, KDSETLED, all_on)
    time.sleep(1)
    fcntl.ioctl(console_fd, KDSETLED, all_off)
    time.sleep(1)

回答by uri

To set CAPS LOCK to a specific value using SendKeys it is important to first detect the state of CAPS LOCK. Here's how to do that in python (under windows):

要使用 SendKeys 将 CAPS LOCK 设置为特定值,首先检测 CAPS LOCK 的状态很重要。以下是在 python 中(在 Windows 下)中执行此操作的方法:

import win32api,win32con

def IsCapsLockOn():
    # return 1 if CAPSLOCK is ON
    return win32api.GetKeyState(win32con.VK_CAPITAL)

回答by Gabriel Staples

For Windows:

对于 Windows:

#https://stackoverflow.com/questions/21549847/send-key-combination-with-python
#https://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx 

import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
wsh.SendKeys("abc") #types out abc directly into wherever you have your cursor (ex: right into this editor itself!)

wsh.SendKeys("{NUMLOCK}{CAPSLOCK}{SCROLLLOCK}") #toggles the state of NumLock, CapsLock, and ScrollLock; remove whichever one you don't want to toggle

Sources:

资料来源:

  1. Send key combination with python
  2. https://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx
  1. 用python发送组合键
  2. https://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx

Also, pay careful attention to Uri's answer about how to read the CapsLock state. To set an LED state specifically to true or false, you can't just blindly toggle, you have to know what the current state is first. He shows you how to read the CapsLock state. Here's how to read all 3 LED states:

另外,请注意 Uri 关于如何读取 CapsLock 状态的回答。要将 LED 状态专门设置为 true 或 false,您不能只是盲目地切换,您必须首先知道当前状态是什么。他向您展示了如何读取 CapsLock 状态。以下是读取所有 3 个 LED 状态的方法:

#https://stackoverflow.com/questions/854393/change-keyboard-locks-in-python/854442#854442abc
#https://support.microsoft.com/en-us/kb/177674
import win32api,win32con

def isCapsLockOn():
    "return 1 if CapsLock is ON"
    return win32api.GetKeyState(win32con.VK_CAPITAL)

def isNumLockOn():
    "return 1 if NumLock is ON"
    return win32api.GetKeyState(win32con.VK_NUMLOCK)

def isScrollLockOn():
    "return 1 if ScrollLock is ON"
    return win32api.GetKeyState(win32con.VK_SCROLL)

print("IsCapsLockOn = ", IsCapsLockOn())
print("isNumLockOn = ", isNumLockOn())
print("isScrollLockOn = ", isScrollLockOn())

For Linux:

对于 Linux:

Don't know all the details yet, but start here: Change keyboard locks in Python.

还不知道所有细节,但从这里开始:在 Python 中更改键盘锁

Is there a generic Python module for any OS?

是否有适用于任何操作系统的通用 Python 模块?

Don't know yet.

还不知道。

Related:

有关的:

  1. How to generate keyboard events in Python?- I need to look into this.
  2. https://pypi.org/project/keyboard/- looks like it requires sudofor Linux. That's not good, but I need to look into it more.
  3. Another Windows-only module it looks like: https://pypi.org/project/SendKeys/
  1. 如何在 Python 中生成键盘事件?- 我需要调查一下。
  2. https://pypi.org/project/keyboard/- 看起来它需要sudoLinux。这不好,但我需要更多地研究它。
  3. 另一个仅适用于 Windows 的模块,它看起来像:https: //pypi.org/project/SendKeys/

回答by Faisal Khan

Probably of no use to the OP but worth sharing as someone might be looking for the answer as i was but could not find the solution without using third party modules. This is what i did to turn the caps lock on

可能对 OP 没有用,但值得分享,因为有人可能会像我一样寻找答案,但如果不使用第三方模块就找不到解决方案。这就是我打开大写锁定所做的工作

import ctypes

def turn_capslock():
    dll = ctypes.WinDLL('User32.dll')
    VK_CAPITAL = 0X14
    if not dll.GetKeyState(VK_CAPITAL):
        dll.keybd_event(VK_CAPITAL, 0X3a, 0X1, 0)
        dll.keybd_event(VK_CAPITAL, 0X3a, 0X3, 0)

    return dll.GetKeyState(VK_CAPITAL)
print(turn_capslock())