python禁用键盘鼠标_linux系统下,python 调用终端禁用鼠标键盘

本文介绍如何在Linux系统中使用终端禁用和启用键盘鼠标,并提供通过Python调用终端命令实现设备禁用的完整代码示例。利用`xinput`命令可以方便地管理输入设备,Python代码则能自动化这一过程。
摘要由CSDN通过智能技术生成

一.在linux系统下使用终端禁用设备

二.使用python调用终端禁用设备.

三.完整的代码

一.在linux系统下使用终端禁用设备

1.在终端中输入 xinpot list   会显示所有的输入设备.如下表:有鼠标,键盘,无线连接,摄像头,本机键盘,触摸屏等等.

~$ xinput list

⎡ Virtual core pointer id=2[master pointer (3)]

⎜ ↳ Virtual core XTEST pointer id=4[slave pointer (2)]

⎜ ↳ ETPS/2 Elantech Touchpad id=18[slave pointer (2)]

⎜ ↳ RAPOO Rapoo 2.4G Wireless Device id=11[slave pointer (2)]

⎜ ↳ RAPOO Rapoo 2.4G Wireless Device id=12[slave pointer (2)]

⎜ ↳ Gaming Mouse id=14[slave pointer (2)]

⎜ ↳ Gaming Mouse id=15[slave pointer (2)]

⎣ Virtual core keyboard id=3[master keyboard (2)]

↳ Virtual core XTEST keyboard id=5[slave keyboard (3)]

↳ Power Button id=6[slave keyboard (3)]

↳ Video Bus id=7[slave keyboard (3)]

↳ Video Bus id=8[slave keyboard (3)]

↳ Power Button id=9[slave keyboard (3)]

↳ Lanseyaoji: Lanseyaoji id=10[slave keyboard (3)]

↳ HP Wide Vision HD Camera: HP Wi id=16[slave keyboard (3)]

↳ RAPOO Rapoo 2.4G Wireless Device id=13[slave keyboard (3)]

↳ AT Translated Set 2 keyboard id=17[slave keyboard (3)]

↳ HP Wireless hotkeys id=19[slave keyboard (3)]

↳ HP WMI hotkeys

2.使用xinput  禁用或启用某个设备(需要的ubuntu终端中输入)

xinput disable 5 #禁用id=5 的输入设备

xinput enable 5 #启用id=5 的输入设备

二.使用python调用终端禁用设备.

1.在python 中运行下面的代码就会输出上面的输入设备信息.

command = "xinput list "

back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

data = back[0].decode() # 获取终端的消息.

print(data)

2.使用python 代码禁用ubuntu设备:

os.system('xinput disable 5' ) #禁用

os.system('xinput enable 5') #启用

三.完整的代码

import subprocess

close_Mouse_key= []

def close_mouse_and_key():

command = "xinput list "

back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

data = back[0].decode() # 获取终端的消息.

datas = re.compile('↳(.*?)\[').findall(data)

for da in datas:

da = da.lower()

if da.find("virtual core xtest keyboard") >= 0:

pass

elif da.find('keyboard') >= 0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

close_Mouse_key.append(d[0])

os.system(text) # 禁用键盘

elif da.find('touchpad') >= 0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

print(text)

#subprocess.Popen(text, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

os.system(text)

close_Mouse_key.append(d[0])

# 禁用触摸屏

elif da.find("mouse") >= 0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

close_Mouse_key.append(d[0])

os.system(text) # 禁用鼠标

elif da.find("wireless device") >= 0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

close_Mouse_key.append(d[0])

os.system(text) # 禁用无线连接

elif da.find("hotkeys")>=0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

close_Mouse_key.append(d[0])

os.system(text) # 禁用热键

elif da.find("usb")>=0:

d = re.compile('id=(.*?)\t').findall(da)

text = 'xinput disable ' + d[0]

close_Mouse_key.append(d[0])

os.system(text) # 禁用usb接口

def open_mouse_and_key():

for sign in close_Mouse_key:

text = 'xinput enable ' + sign

os.system(text) # 启用

close_mouse_and_key()

time.sleep(20)

open_mouse_and_key()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值