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

  • 一.在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()

 

你可能感兴趣的:(python)