@elyd

Python. Почему после компиляции появляется ошибка связанная с pynput.keyboard (до компиляции таковой нет)?

Вот сама ошибка:

Traceback (most recent call last):
  File "layout.py", line 7, in <module>
    from pynput.keyboard import Listener, KeyCode, Key, Controller
  File "c:\program files (x86)\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pynput\__init__.py", line 40, in <module>
  File "c:\program files (x86)\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pynput\keyboard\__init__.py", line 31, in <module>
  File "pynput\_util\__init__.py", line 76, in backend
ImportError
[11436] Failed to execute script

Пишу на Python 3.8.5. Что делать - не представляю.
Код:

import os
import sys
import time
import ctypes
import threading
import pyperclip
from pynput.keyboard import Listener, KeyCode, Key, Controller

keyboard=Controller()
pressed_vks=set()

entoru={'~': 'Ё', 'Q': 'Й', 'W': 'Ц', 'E': 'У', 'R': 'К', 'T': 'Е', 'Y': 'Н', 'U': 'Г', 'I': 'Ш', 'O': 'Щ', 'P': 'З', '{': 'Х', '}': 'Ъ', 'A': 'Ф', 'S': 'Ы', 'D': 'В', 'F': 'А', 'G': 'П', 'H': 'Р', 'J': 'О', 'K': 'Л', 'L': 'Д', ':': 'Ж', '"': 'Э', 'Z': 'Я', 'X': 'Ч', 'C': 'С', 'V': 'М', 'B': 'И', 'N': 'Т', 'M': 'Ь', '<': 'Б', '>': 'Ю', '|': '/', '`': 'ё', 'q': 'й', 'w': 'ц', 'e': 'у', 'r': 'к', 't': 'е', 'y': 'н', 'u': 'г', 'i': 'ш', 'o': 'щ', 'p': 'з', '[': 'х', ']': 'ъ', 'a': 'ф', 's': 'ы', 'd': 'в', 'f': 'а', 'g': 'п', 'h': 'р', 'j': 'о', 'k': 'л', 'l': 'д', ';': 'ж', "'": 'э', 'z': 'я', 'x': 'ч', 'c': 'с', 'v': 'м', 'b': 'и', 'n': 'т', 'm': 'ь', ',': 'б', '.': 'ю', '?': ',', '/': '.', '@': '"', '#': '№', '$': ';', '&': '?', '^': ':'}
rutoen={'Ё': '~', 'Й': 'Q', 'Ц': 'W', 'У': 'E', 'К': 'R', 'Е': 'T', 'Н': 'Y', 'Г': 'U', 'Ш': 'I', 'Щ': 'O', 'З': 'P', 'Х': '{', 'Ъ': '}', 'Ф': 'A', 'Ы': 'S', 'В': 'D', 'А': 'F', 'П': 'G', 'Р': 'H', 'О': 'J', 'Л': 'K', 'Д': 'L', 'Ж': ':', 'Э': '"', 'Я': 'Z', 'Ч': 'X', 'С': 'C', 'М': 'V', 'И': 'B', 'Т': 'N', 'Ь': 'M', 'Б': '<', 'Ю': '>', '/': '|', 'ё': '`', 'й': 'q', 'ц': 'w', 'у': 'e', 'к': 'r', 'е': 't', 'н': 'y', 'г': 'u', 'ш': 'i', 'щ': 'o', 'з': 'p', 'х': '[', 'ъ': ']', 'ф': 'a', 'ы': 's', 'в': 'd', 'а': 'f', 'п': 'g', 'р': 'h', 'о': 'j', 'л': 'k', 'д': 'l', 'ж': ';', 'э': "'", 'я': 'z', 'ч': 'x', 'с': 'c', 'м': 'v', 'и': 'b', 'т': 'n', 'ь': 'm', 'б': ',', 'ю': '.', ',': '?', '.': '/', '"': '@', '№': '#', ';': '$', '?': '&', ':': '^'}
uptolow={'Ё': 'ё', 'Й': 'й', 'Ц': 'ц', 'У': 'у', 'К': 'к', 'Е': 'е', 'Н': 'н', 'Г': 'г', 'Ш': 'ш', 'Щ': 'щ', 'З': 'з', 'Х': 'х', 'Ъ': 'ъ', 'Ф': 'ф', 'Ы': 'ы', 'В': 'в', 'А': 'а', 'П': 'п', 'Р': 'р', 'О': 'о', 'Л': 'л', 'Д': 'д', 'Ж': 'ж', 'Э': 'э', 'Я': 'я', 'Ч': 'ч', 'С': 'с', 'М': 'м', 'И': 'и', 'Т': 'т', 'Ь': 'ь', 'Б': 'б', 'Ю': 'ю', 'Q': 'q', 'W': 'w', 'E': 'e', 'R': 'r', 'T': 't', 'Y': 'y', 'U': 'u', 'I': 'i', 'O': 'o', 'P': 'p', 'A': 'a', 'S': 's', 'D': 'd', 'F': 'f', 'G': 'g', 'H': 'h', 'J': 'j', 'K': 'k', 'L': 'l', 'Z': 'z', 'X': 'x', 'C': 'c', 'V': 'v', 'B': 'b', 'N': 'n', 'M': 'm'}
lowtoup={'ё': 'Ё', 'й': 'Й', 'ц': 'Ц', 'у': 'У', 'к': 'К', 'е': 'Е', 'н': 'Н', 'г': 'Г', 'ш': 'Ш', 'щ': 'Щ', 'з': 'З', 'х': 'Х', 'ъ': 'Ъ', 'ф': 'Ф', 'ы': 'Ы', 'в': 'В', 'а': 'А', 'п': 'П', 'р': 'Р', 'о': 'О', 'л': 'Л', 'д': 'Д', 'ж': 'Ж', 'э': 'Э', 'я': 'Я', 'ч': 'Ч', 'с': 'С', 'м': 'М', 'и': 'И', 'т': 'Т', 'ь': 'Ь', 'б': 'Б', 'ю': 'Ю', 'q': 'Q', 'w': 'W', 'e': 'E', 'r': 'R', 't': 'T', 'y': 'Y', 'u': 'U', 'i': 'I', 'o': 'O', 'p': 'P', 'a': 'A', 's': 'S', 'd': 'D', 'f': 'F', 'g': 'G', 'h': 'H', 'j': 'J', 'k': 'K', 'l': 'L', 'z': 'Z', 'x': 'X', 'c': 'C', 'v': 'V', 'b': 'B', 'n': 'N', 'm': 'M'}

EXITCOMBO=[
	{Key.shift, KeyCode(vk=192)}
]

SWITCHKEYCOMBO=[
	{Key.shift, Key.alt_l}
]

SWITCHRGSTRCOMBO=[
	{Key.caps_lock}
]

def on_press(key):
	global pressed_vks
	vk = get_vk(key)
	pressed_vks.add(vk)
	
	for combination in SWITCHKEYCOMBO:
		if is_combination_pressed(combination):
			SKthread=threading.Thread(target=SwitchKeysLanguage(),args=())
			SKthread.start()
			return False
			break
	for combination in SWITCHRGSTRCOMBO:
		if is_combination_pressed(combination):
			SRthread=threading.Thread(target=SwitchKeysRgstr(),args=())
			SRthread.start()
			return False
			break

def on_release(key):
	global pressed_vks
	vk=get_vk(key)
	pressed_vks=set()

def is_combination_pressed(combination):
	return all([get_vk(key) in pressed_vks for key in combination])

def get_vk(key):
	return key.vk if hasattr(key, 'vk') else key.value.vk

def Copy():
	global pressed_vks
	keyboard.press(Key.ctrl)
	keyboard.press(KeyCode(vk=67))
	keyboard.release(KeyCode(vk=67))
	keyboard.release(Key.ctrl)
	pressed_vks=set()
	return pyperclip.paste()

def Paste(msg):
	global pressed_vks
	pyperclip.copy(msg)
	keyboard.press(Key.ctrl)
	keyboard.press(KeyCode(vk=86))
	keyboard.release(KeyCode(vk=86))
	keyboard.release(Key.ctrl)
	pressed_vks=set()

def Language():
	user32=ctypes.WinDLL('user32', use_last_error=True)
	curr_window=user32.GetForegroundWindow()
	thread_id=user32.GetWindowThreadProcessId(curr_window, 0)
	klid=user32.GetKeyboardLayout(thread_id)
	lid=klid & (2**16-1)
	lhex=hex(lid)
	return lhex

def Rgstr():
	user32=ctypes.WinDLL('user32', use_last_error=True)
	rgstr=user32.GetKeyState(0x14)
	return rgstr

def SwitchKeysLanguage():
	global pressed_vks
	pressed_vks=set()
	time.sleep(0.5)
	language=Language()
	if language=='0x419': #Russian
		msg=Correcter(entoru,Copy())
	elif language=='0x409': #English
		msg=Correcter(rutoen,Copy())
	else:
		pass
	time.sleep(0.01)
	Paste(msg)
	listener=Listener(on_press=on_press,on_release=on_release)
	listener.start()

def SwitchKeysRgstr():
	global pressed_vks
	pressed_vks=set()
	time.sleep(0.5)
	rgstr=Rgstr()
	if rgstr==1: #UpperCase
		msg=Correcter(lowtoup,Copy())
	elif rgstr==0: #LowerCase
		msg=Correcter(uptolow,Copy())
	else:
		pass
	time.sleep(0.01)
	Paste(msg)
	listener=Listener(on_press=on_press,on_release=on_release)
	listener.start()

def Correcter(dict,msg):
	halfmsg=''
	for i in range(0,len(msg)):
		tempmsg=msg[0]
		try:
			tempmsg=tempmsg.replace(tempmsg[0],dict[tempmsg[0]],1)
		except:
			pass
		halfmsg=halfmsg+tempmsg
		msg=msg[1:]
	return halfmsg

def kill(key):
	vk = get_vk(key)
	pressed_vks.add(vk) 
	for combination in EXITCOMBO:
		if is_combination_pressed(combination):
			return False

def ListenerFunction():
	with Listener(on_press=kill, on_release=kill) as listener:
		listener.join()

def LifeCycle():
	ListenerThread=threading.Thread(target=ListenerFunction,args=())
	ListenerThread.start()
	sys.exit()

listener=Listener(on_press=on_press,on_release=on_release)
listener.start()

LifeCycle()
  • Вопрос задан
  • 164 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы