KEYS UPDATE
UPDATE: - Moved from keyboard module to pynput.Listner, now keys work better - Refactored sound_get function - Little fixes
This commit is contained in:
parent
5ed7c7d2c8
commit
8181dfa28f
1 changed files with 95 additions and 74 deletions
159
main.py
159
main.py
|
@ -3,11 +3,13 @@ import sys
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
import keyboard
|
import keyboard
|
||||||
|
from pynput.keyboard import Listener
|
||||||
import soundfile as sf
|
import soundfile as sf
|
||||||
import sounddevice as sd
|
import sounddevice as sd
|
||||||
from PyQt5 import QtWidgets, QtGui, QtCore
|
from PyQt5 import QtWidgets, QtGui, QtCore
|
||||||
import ui_sundpood
|
import ui_sundpood
|
||||||
import ui_overlay
|
import ui_overlay
|
||||||
|
import keys
|
||||||
|
|
||||||
class OverlayUi(QtWidgets.QMainWindow, ui_overlay.Ui_MainWindow):
|
class OverlayUi(QtWidgets.QMainWindow, ui_overlay.Ui_MainWindow):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
|
@ -29,6 +31,7 @@ class MainUi(QtWidgets.QMainWindow, ui_sundpood.Ui_MainWindow):
|
||||||
self.hide()
|
self.hide()
|
||||||
over.show()
|
over.show()
|
||||||
|
|
||||||
|
|
||||||
###! JSON !###
|
###! JSON !###
|
||||||
def jsonread(file): ## Чтение JSON
|
def jsonread(file): ## Чтение JSON
|
||||||
with open(file, "r", encoding='utf-8') as read_file:
|
with open(file, "r", encoding='utf-8') as read_file:
|
||||||
|
@ -50,70 +53,80 @@ def found_device(list_): # Поиск микшера VoiceMee
|
||||||
return index
|
return index
|
||||||
|
|
||||||
def sound_get(mode): # Сбор файлов
|
def sound_get(mode): # Сбор файлов
|
||||||
|
def check_format(name, format_): # Проверка слова до точки с конца строки
|
||||||
|
suf = ''
|
||||||
|
while name[-1] != '.':
|
||||||
|
suf += name[-1]
|
||||||
|
name = name[:-1]
|
||||||
|
suf = suf[::-1]
|
||||||
|
if type(format_) is list:
|
||||||
|
if suf in format_:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
elif type(format_) is str:
|
||||||
|
if suf == format_:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
if os.path.exists('settings.json') and mode == False:
|
def sound_convert(path, name, format_): # Конвертация из форматов 'mp3' 'm4a' в format_
|
||||||
sounds_list = jsonread('settings.json')
|
old_name = name
|
||||||
|
if check_format(name, ['mp3', 'm4a']):
|
||||||
|
while name[-1] != '.':
|
||||||
|
name = name[:-1]
|
||||||
|
name += format_
|
||||||
|
|
||||||
|
os.system(f'''ffmpeg.exe -i "{os.path.join(path, old_name)}"
|
||||||
|
"{os.path.join(path, name)}"''')
|
||||||
|
os.remove(f'{os.path.join(path, old_name)}')
|
||||||
|
return name
|
||||||
|
|
||||||
elif not os.path.exists('settings.json') or mode == True:
|
|
||||||
msg = QtWidgets.QMessageBox()
|
msg = QtWidgets.QMessageBox()
|
||||||
msg.setIcon(QtWidgets.QMessageBox.Critical)
|
msg.setIcon(QtWidgets.QMessageBox.Critical)
|
||||||
msg.setText("You don't have any sounds in 'sound' folder")
|
msg.setText("You don't have any sounds in 'sound' folder")
|
||||||
msg.setInformativeText('download sound in .wav / .mp3 / .m4a format')
|
msg.setInformativeText('download sound in .wav / .mp3 / .m4a format')
|
||||||
msg.setWindowTitle('Error')
|
msg.setWindowTitle('Error')
|
||||||
|
|
||||||
if os.path.exists('sound'):
|
if os.path.exists('settings.json') and mode == False:
|
||||||
sounds = []
|
sounds_list = jsonread('settings.json')
|
||||||
for i in os.listdir('sound'):
|
|
||||||
format_ = ''
|
|
||||||
name = i
|
|
||||||
if os.path.isfile(os.path.join('sound', i)):
|
|
||||||
while i[-1] != '.':
|
|
||||||
format_ += i[-1]
|
|
||||||
i = i[:-1]
|
|
||||||
format_ = format_[::-1]
|
|
||||||
if format_ == 'wav':
|
|
||||||
sounds.append(name)
|
|
||||||
|
|
||||||
if len(sounds) == 0:
|
elif not os.path.exists('settings.json') or mode == True:
|
||||||
|
if os.path.exists('sound'):
|
||||||
|
if len(os.listdir('sound')) == 0:
|
||||||
msg.exec_()
|
msg.exec_()
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
menu = []
|
menu = []
|
||||||
sounds_list = ['sound\\']
|
sounds_list = ['sound\\']
|
||||||
for i in os.listdir('sound'): # Коонвертируем файлы в .wav
|
for i in os.listdir('sound'):
|
||||||
name = i
|
|
||||||
format_ = ''
|
|
||||||
if os.path.isfile(os.path.join('sound', i)):
|
if os.path.isfile(os.path.join('sound', i)):
|
||||||
while i[-1] != '.':
|
i = sound_convert('sound', i, 'wav')
|
||||||
format_ += i[-1]
|
sounds_list.append(i)
|
||||||
i = i[:-1]
|
|
||||||
format_ = format_[::-1]
|
|
||||||
if format_ in ['mp3', 'm4a']:
|
|
||||||
os.system(f'ffmpeg.exe -i "sound\\{name}" "sound\\{i}wav"')
|
|
||||||
os.remove(f'sound\\{name}')
|
|
||||||
sounds_list.append(name)
|
|
||||||
else:
|
else:
|
||||||
sounds_list_cat = [os.path.join('sound', i)]
|
sounds_list_cat = [os.path.join('sound', i)]
|
||||||
for x in os.listdir(os.path.join('sound', i)): # Коонвертируем файлы в .wav
|
for x in os.listdir(os.path.join('sound', i)):
|
||||||
name = x
|
|
||||||
format_ = ''
|
|
||||||
if os.path.isfile(os.path.join('sound', i, x)):
|
if os.path.isfile(os.path.join('sound', i, x)):
|
||||||
while x[-1] != '.':
|
x = sound_convert(os.path.join('sound', i), x, 'wav')
|
||||||
format_ += x[-1]
|
sounds_list_cat.append(x)
|
||||||
x = x[:-1]
|
|
||||||
format_ = format_[::-1]
|
|
||||||
if format_ in ['mp3', 'm4a']:
|
|
||||||
os.system(f'ffmpeg.exe -i "sound\\{os.path.join(i, name)}" "sound\\{os.path.join(i, x)}wav"')
|
|
||||||
os.remove(f'sound\\{os.path.join(i, name)}')
|
|
||||||
sounds_list_cat.append(name)
|
|
||||||
menu.append(sounds_list_cat)
|
menu.append(sounds_list_cat)
|
||||||
menu.append(sounds_list)
|
menu.append(sounds_list)
|
||||||
|
|
||||||
|
sounds = []
|
||||||
|
for i in os.listdir('sound'):
|
||||||
|
if os.path.isfile(os.path.join('sound', i)):
|
||||||
|
if check_format(i, 'wav'):
|
||||||
|
sounds.append(i)
|
||||||
|
|
||||||
if os.path.exists('settings.json'):
|
if os.path.exists('settings.json'):
|
||||||
hotkeys = jsonread('settings.json')['hotkeys']
|
hotkeys = jsonread('settings.json')['hotkeys']
|
||||||
sounds_list = {'sounds':sounds, 'hotkeys':hotkeys, 'menu':menu}
|
sounds_list = {'sound':sounds,
|
||||||
|
'hotkeys':hotkeys,
|
||||||
|
'menu':menu}
|
||||||
else:
|
else:
|
||||||
sounds_list = {'sounds':sounds, 'hotkeys':{'':''}, 'menu':menu}
|
sounds_list = {'sounds':sounds,
|
||||||
|
'hotkeys':{'Push button':''},
|
||||||
|
'menu':menu}
|
||||||
|
|
||||||
for i in COMBOS:
|
for i in COMBOS:
|
||||||
i.addItems(sounds)
|
i.addItems(sounds)
|
||||||
|
@ -127,9 +140,7 @@ def save(): # Сохранение списка
|
||||||
hotkeys = {}
|
hotkeys = {}
|
||||||
sounds = sound_get(False)
|
sounds = sound_get(False)
|
||||||
for i in range(len(COMBOS)):
|
for i in range(len(COMBOS)):
|
||||||
print(COMBOS[i].currentText())
|
|
||||||
hotkeys.setdefault(HOTKEYS[i].text(), COMBOS[i].currentText())
|
hotkeys.setdefault(HOTKEYS[i].text(), COMBOS[i].currentText())
|
||||||
print(hotkeys)
|
|
||||||
|
|
||||||
sounds_list = {'sounds':sounds, 'hotkeys':hotkeys, 'menu':menu}
|
sounds_list = {'sounds':sounds, 'hotkeys':hotkeys, 'menu':menu}
|
||||||
jsonwrite('settings.json', sounds_list)
|
jsonwrite('settings.json', sounds_list)
|
||||||
|
@ -137,6 +148,7 @@ def save(): # Сохранение списка
|
||||||
hotkeys = None
|
hotkeys = None
|
||||||
|
|
||||||
def play_sound(index): # Проигрываение звука
|
def play_sound(index): # Проигрываение звука
|
||||||
|
print(index)
|
||||||
try:
|
try:
|
||||||
filename = COMBOS[index].currentText()
|
filename = COMBOS[index].currentText()
|
||||||
try:
|
try:
|
||||||
|
@ -156,17 +168,30 @@ def play_sound(index): # Проигрываение зву
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def hotkey_remap(btn):
|
def hotkey_remap(btn): # Переназначение хоткеев
|
||||||
|
def check(key):
|
||||||
button = HOTKEYS[btn]
|
button = HOTKEYS[btn]
|
||||||
key = keyboard.read_key()
|
key = str(key)
|
||||||
if key not in ['esc']:
|
if key not in keys.forbidden:
|
||||||
print(key)
|
print(key)
|
||||||
print(COMMANDS[COMMANDS.index(button.text())])
|
COMMANDS[COMMANDS.index(button.text())] = key.replace("'",'')
|
||||||
COMMANDS[COMMANDS.index(button.text())] = key
|
button.setText(key.replace("'",''))
|
||||||
button.setText(key)
|
for i in HOTKEYS:
|
||||||
|
if i != HOTKEYS[btn]:
|
||||||
|
i.setEnabled(True)
|
||||||
|
return False
|
||||||
|
|
||||||
|
for i in HOTKEYS:
|
||||||
|
if i != HOTKEYS[btn]:
|
||||||
|
i.setEnabled(False)
|
||||||
|
|
||||||
|
hotkey_remap_Listener = Listener(
|
||||||
|
on_release=check)
|
||||||
|
hotkey_remap_Listener.start()
|
||||||
|
|
||||||
|
|
||||||
###! CONTROL !###
|
###! CONTROL !###
|
||||||
def key(arg): # Хоткеи
|
def key_check(key): # Хоткеи
|
||||||
def select_move(mode):
|
def select_move(mode):
|
||||||
select[1] += mode[1]
|
select[1] += mode[1]
|
||||||
select[0] += mode[0]
|
select[0] += mode[0]
|
||||||
|
@ -179,6 +204,10 @@ def key(arg): # Хоткеи
|
||||||
over.label.setText(menu[select[0]][select[1]])
|
over.label.setText(menu[select[0]][select[1]])
|
||||||
win.select_label.setText(menu[select[0]][select[1]])
|
win.select_label.setText(menu[select[0]][select[1]])
|
||||||
|
|
||||||
|
key = str(key)
|
||||||
|
if key.replace("'",'') in COMMANDS:
|
||||||
|
play_sound(COMMANDS.index(key.replace("'",'')))
|
||||||
|
|
||||||
keyboard.add_hotkey(72, select_move, args=[[0, -1]])
|
keyboard.add_hotkey(72, select_move, args=[[0, -1]])
|
||||||
keyboard.add_hotkey(80, select_move, args=[[0, 1]])
|
keyboard.add_hotkey(80, select_move, args=[[0, 1]])
|
||||||
keyboard.add_hotkey(77, select_move, args=[[1, 0]])
|
keyboard.add_hotkey(77, select_move, args=[[1, 0]])
|
||||||
|
@ -186,21 +215,11 @@ def key(arg): # Хоткеи
|
||||||
keyboard.add_hotkey(76, play_sound, args=[''])
|
keyboard.add_hotkey(76, play_sound, args=[''])
|
||||||
keyboard.add_hotkey(73, sd.stop)
|
keyboard.add_hotkey(73, sd.stop)
|
||||||
|
|
||||||
while True:
|
|
||||||
key = keyboard.read_key()
|
|
||||||
print(keyboard.key_to_scan_codes(key))
|
|
||||||
if key == 'esc':
|
|
||||||
break
|
|
||||||
elif key in COMMANDS:
|
|
||||||
print(key)
|
|
||||||
play_sound(COMMANDS.index(key))
|
|
||||||
|
|
||||||
|
|
||||||
def main(): # Интерфейс
|
def main(): # Интерфейс
|
||||||
|
win.show()
|
||||||
|
key_check_Listener.start()
|
||||||
|
|
||||||
hotkeys_thread.start()
|
|
||||||
win.save_button.clicked.connect(save)
|
win.save_button.clicked.connect(save)
|
||||||
|
|
||||||
win.hotkey_1.clicked.connect(lambda: hotkey_remap(0))
|
win.hotkey_1.clicked.connect(lambda: hotkey_remap(0))
|
||||||
win.hotkey_2.clicked.connect(lambda: hotkey_remap(1))
|
win.hotkey_2.clicked.connect(lambda: hotkey_remap(1))
|
||||||
win.hotkey_3.clicked.connect(lambda: hotkey_remap(2))
|
win.hotkey_3.clicked.connect(lambda: hotkey_remap(2))
|
||||||
|
@ -214,6 +233,7 @@ def main(): # Интерфейс
|
||||||
win.hotkey_11.clicked.connect(lambda: hotkey_remap(10))
|
win.hotkey_11.clicked.connect(lambda: hotkey_remap(10))
|
||||||
win.hotkey_12.clicked.connect(lambda: hotkey_remap(11))
|
win.hotkey_12.clicked.connect(lambda: hotkey_remap(11))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
### Поиск устроства ввода ###
|
### Поиск устроства ввода ###
|
||||||
list_ = list(sd.query_devices())
|
list_ = list(sd.query_devices())
|
||||||
|
@ -224,9 +244,6 @@ if __name__ == '__main__':
|
||||||
app = QtWidgets.QApplication([])
|
app = QtWidgets.QApplication([])
|
||||||
over = OverlayUi()
|
over = OverlayUi()
|
||||||
win = MainUi()
|
win = MainUi()
|
||||||
win.show()
|
|
||||||
|
|
||||||
select = [0, 0]
|
|
||||||
COMBOS = [
|
COMBOS = [
|
||||||
win.combo0,
|
win.combo0,
|
||||||
win.combo1,
|
win.combo1,
|
||||||
|
@ -254,13 +271,19 @@ if __name__ == '__main__':
|
||||||
win.hotkey_11,
|
win.hotkey_11,
|
||||||
win.hotkey_12,]
|
win.hotkey_12,]
|
||||||
|
|
||||||
sounds = sound_get(True)['hotkeys']
|
sound_get_dict = sound_get(True)
|
||||||
|
|
||||||
|
### Глобальные переменные ###
|
||||||
|
sounds = sound_get_dict['hotkeys']
|
||||||
|
menu = sound_get_dict['menu']
|
||||||
|
select = [0, 0]
|
||||||
combo = 0
|
combo = 0
|
||||||
for i in sounds.items():
|
for i in sounds.items():
|
||||||
index = COMBOS[combo].findText(i[1])
|
index = COMBOS[combo].findText(i[1])
|
||||||
COMBOS[combo].setCurrentIndex(index)
|
COMBOS[combo].setCurrentIndex(index)
|
||||||
HOTKEYS[combo].setText(i[0])
|
HOTKEYS[combo].setText(i[0])
|
||||||
combo += 1
|
combo += 1
|
||||||
|
combo = None
|
||||||
|
|
||||||
COMMANDS = [
|
COMMANDS = [
|
||||||
HOTKEYS[0].text(),
|
HOTKEYS[0].text(),
|
||||||
|
@ -276,10 +299,8 @@ if __name__ == '__main__':
|
||||||
HOTKEYS[10].text(),
|
HOTKEYS[10].text(),
|
||||||
HOTKEYS[11].text(),]
|
HOTKEYS[11].text(),]
|
||||||
|
|
||||||
menu = sound_get(True)['menu']
|
key_check_Listener = Listener(
|
||||||
|
on_release=key_check)
|
||||||
hotkeys_thread = threading.Thread(target=key, args=(1,))
|
|
||||||
hotkeys_thread.setDaemon(True)
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
sys.exit(app.exec())
|
sys.exit(app.exec())
|
||||||
|
|
Loading…
Add table
Reference in a new issue