-
Notifications
You must be signed in to change notification settings - Fork 4
/
MorseCodeGUI.py
1759 lines (1487 loc) · 78.8 KB
/
MorseCodeGUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Standard library imports
import configparser
import json
import logging
import os
import sys
import platform
import threading
import time
from collections import OrderedDict
from enum import Enum
from threading import Thread
# Third-party imports
from PyQt5 import QtCore, QtMultimedia
from PyQt5.QtMultimedia import QAudioDeviceInfo, QAudio, QAudioFormat, QAudioOutput
from PyQt5.QtCore import QIODevice, QFile, QThread, pyqtSignal, QTimer, Qt, QObject
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QAction, QCheckBox, QComboBox, QDialog, QGridLayout,
QGroupBox, QHBoxLayout, QLabel, QLineEdit, QMessageBox,
QPushButton, QRadioButton, QSystemTrayIcon, QVBoxLayout,
QWidget, QApplication, QMenu, QFileDialog, QStatusBar)
from nava import play
import keyboard
import mouse
# Local application/library specific imports
import pressagio.callback
import pressagio
import icons_rc
def get_user_data_dir(app_name="MorseWriter"):
"""
Returns the appropriate directory for storing user data based on the OS and whether the app is frozen.
"""
if hasattr(sys, 'frozen'):
# If the application is frozen, use the appropriate platform-specific directory
if platform.system() == 'Windows':
return os.path.join('C:\\', 'Users', 'Public', 'Documents', 'Ace Centre', app_name, 'user_data')
elif platform.system() == 'Darwin':
return os.path.join(os.path.expanduser('~/Library/Application Support/'), app_name, 'user_data')
else:
return os.path.join(os.path.expanduser('~/.config/'), app_name, 'user_data')
else:
# Use a local directory when running in development
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'user_data')
# Configure basic logger
logfile = os.path.join(get_user_data_dir(), "morsewriter-log.log")
logging.basicConfig(level=logging.DEBUG, filename=logfile, filemode='w',format='%(name)s - %(levelname)s - %(message)s')
# # If you want to the console
# logging.basicConfig(level=logging.DEBUG,format='%(name)s - %(levelname)s - %(message)s')
lastkeydowntime = -1
keystrokes_state = {}
currentX = 0
currentY = 0
pressingKey = False
typestate = None
# If configfile file is lost..
DEFAULT_CONFIG = {
"keylen": 1,
"keyone": "SPACE",
"keytwo": "ENTER",
"keythree": "RCTRL",
"maxDitTime": 350, # It's better to store numbers as numbers, not strings
"minLetterPause": 1000,
"withsound": True,
"SoundDit": "res/dit_sound.wav",
"SoundDah": "res/dah_sound.wav",
"SoundTyping": "res/typing_sound.wav",
"debug": True,
"off": False,
"fontsizescale": 100,
"upperchars": True,
"autostart": False,
"winxaxis": "left",
"winyaxis": "top",
"winposx": 10,
"winposy": 10
}
class AudioDeviceSelector(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Select Audio Device')
self.layout = QVBoxLayout()
self.label = QLabel('Select Audio Device:')
self.layout.addWidget(self.label)
self.device_selector = QComboBox()
self.list_available_devices()
self.device_selector.currentIndexChanged.connect(self.device_changed)
self.layout.addWidget(self.device_selector)
self.test_audio_button = QPushButton('Play Test Sound')
self.test_audio_button.clicked.connect(self.test_audio)
self.layout.addWidget(self.test_audio_button)
self.setLayout(self.layout)
self.sound_file = None
self.player = QtMultimedia.QMediaPlayer()
self.selected_device = self.device_selector.currentData()
icon = QIcon(':/morse-writer.ico')
self.setWindowIcon(icon)
def device_changed(self, index):
self.selected_device = self.device_selector.itemData(index)
print(f"Selected device audio device {index} : {self.selected_device.deviceName()}")
def list_available_devices(self):
default_device = QAudioDeviceInfo.defaultOutputDevice()
self.device_selector.addItem(default_device.deviceName(), default_device)
for device in QAudioDeviceInfo.availableDevices(QAudio.AudioOutput):
device_names = [self.device_selector.itemText(i) for i in range(self.device_selector.count())]
if device.deviceName() not in device_names:
self.device_selector.addItem(device.deviceName(), device)
def play_audio(self, file):
# check here if file exist
if os.path.exists(file):
url = QtCore.QUrl.fromLocalFile(QtCore.QDir.current().absoluteFilePath(file))
self.player.setMedia(QtMultimedia.QMediaContent(url))
self.player.play()
def test_audio(self):
self.play_audio("res/dah_sound.wav")
def select_audio_file(self):
file_dialog = QFileDialog()
self.audio_file, _ = file_dialog.getOpenFileName(self, "Select Audio File", "", "Audio Files (*.wav)")
if self.audio_file:
print(f"Selected audio file: {self.audio_file} to play on device {self.selected_device.deviceName()}")
self.play_audio(self.audio_file)
@staticmethod
def load_abbreviations(file_path):
abbreviations = {}
try:
logging.debug(f"[TypeState] Trying to load abbreviations from file: {file_path}")
with open(file_path, 'r') as f:
for line in f:
if line.strip():
abbr, expansion = line.strip().split('\t')
abbreviations[abbr] = expansion
except Exception as e:
logging.error(f"Failed to load abbreviations: {e}")
return abbreviations
@staticmethod
def expand_abbreviation(keys, abbreviations):
words = keys.split()
last_word = words[-1]
if last_word in abbreviations:
return abbreviations[last_word], len(last_word)
else:
return None,None
class ConfigManager:
def __init__(self, config_file=None, default_config=DEFAULT_CONFIG):
self.key_data = {
"A": {'label': 'a', 'key_code': 'a', 'character': 'a', 'arg': None},
"B": {'label': 'b', 'key_code': 'b', 'character': 'b', 'arg': None},
"C": {'label': 'c', 'key_code': 'c', 'character': 'c', 'arg': None},
"D": {'label': 'd', 'key_code': 'd', 'character': 'd', 'arg': None},
"E": {'label': 'e', 'key_code': 'e', 'character': 'e', 'arg': None},
"F": {'label': 'f', 'key_code': 'f', 'character': 'f', 'arg': None},
"G": {'label': 'g', 'key_code': 'g', 'character': 'g', 'arg': None},
"H": {'label': 'h', 'key_code': 'h', 'character': 'h', 'arg': None},
"I": {'label': 'i', 'key_code': 'i', 'character': 'i', 'arg': None},
"J": {'label': 'j', 'key_code': 'j', 'character': 'j', 'arg': None},
"K": {'label': 'k', 'key_code': 'k', 'character': 'k', 'arg': None},
"L": {'label': 'l', 'key_code': 'l', 'character': 'l', 'arg': None},
"M": {'label': 'm', 'key_code': 'm', 'character': 'm', 'arg': None},
"N": {'label': 'n', 'key_code': 'n', 'character': 'n', 'arg': None},
"O": {'label': 'o', 'key_code': 'o', 'character': 'o', 'arg': None},
"P": {'label': 'p', 'key_code': 'p', 'character': 'p', 'arg': None},
"Q": {'label': 'q', 'key_code': 'q', 'character': 'q', 'arg': None},
"R": {'label': 'r', 'key_code': 'r', 'character': 'r', 'arg': None},
"S": {'label': 's', 'key_code': 's', 'character': 's', 'arg': None},
"T": {'label': 't', 'key_code': 't', 'character': 't', 'arg': None},
"U": {'label': 'u', 'key_code': 'u', 'character': 'u', 'arg': None},
"V": {'label': 'v', 'key_code': 'v', 'character': 'v', 'arg': None},
"W": {'label': 'w', 'key_code': 'w', 'character': 'w', 'arg': None},
"X": {'label': 'x', 'key_code': 'x', 'character': 'x', 'arg': None},
"Y": {'label': 'y', 'key_code': 'y', 'character': 'y', 'arg': None},
"Z": {'label': 'z', 'key_code': 'z', 'character': 'z', 'arg': None},
"ONE": {'label': '1', 'key_code': '1', 'character': '1', 'arg': None},
"TWO": {'label': '2', 'key_code': '2', 'character': '2', 'arg': None},
"THREE": {'label': '3', 'key_code': '3', 'character': '3', 'arg': None},
"FOUR": {'label': '4', 'key_code': '4', 'character': '4', 'arg': None},
"FIVE": {'label': '5', 'key_code': '5', 'character': '5', 'arg': None},
"SIX": {'label': '6', 'key_code': '6', 'character': '6', 'arg': None},
"SEVEN": {'label': '7', 'key_code': '7', 'character': '7', 'arg': None},
"EIGHT": {'label': '8', 'key_code': '8', 'character': '8', 'arg': None},
"NINE": {'label': '9', 'key_code': '9', 'character': '9', 'arg': None},
"ZERO": {'label': '0', 'key_code': '0', 'character': '0', 'arg': None},
"DOT": {'label': '.', 'key_code': '.', 'character': '.', 'arg': None},
"COMMA": {'label': ',', 'key_code': ',', 'character': ',', 'arg': None},
"QUESTION": {'label': '?', 'key_code': '/', 'character': '?', 'arg': None},
"EXCLAMATION": {'label': '!', 'key_code': '1', 'character': '!', 'arg': None},
"COLON": {'label': ':', 'key_code': ';', 'character': ':', 'arg': None},
"SEMICOLON": {'label': ';', 'key_code': ';', 'character': ';', 'arg': None},
"AT": {'label': '@', 'key_code': '2', 'character': '@', 'arg': None},
"HASH": {'label': '#', 'key_code': '3', 'character': '#', 'arg': None},
"DOLLAR": {'label': '$', 'key_code': '4', 'character': '$', 'arg': None},
"PERCENT": {'label': '%', 'key_code': '5', 'character': '%', 'arg': None},
"AMPERSAND": {'label': '&', 'key_code': '7', 'character': '&', 'arg': None},
"STAR": {'label': '*', 'key_code': '*', 'character': '*', 'arg': None},
"PLUS": {'label': '+', 'key_code': '=', 'character': '+', 'arg': None},
"MINUS": {'label': '-', 'key_code': '-', 'character': '-', 'arg': None},
"EQUALS": {'label': '=', 'key_code': '=', 'character': '=', 'arg': None},
"FSLASH": {'label': '/', 'key_code': '/', 'character': '/', 'arg': None},
"BSLASH": {'label': '\\', 'key_code': '\\', 'character': '\\', 'arg': None},
"SINGLEQUOTE": {'label': "'", 'key_code': "'", 'character': "'", 'arg': None},
"DOUBLEQUOTE": {'label': '"', 'key_code': '"', 'character': '"', 'arg': None},
"OPENBRACKET": {'label': '(', 'key_code': '9', 'character': '(', 'arg': None},
"CLOSEBRACKET": {'label': ')', 'key_code': '0', 'character': ')', 'arg': None},
"LESSTHAN": {'label': '<', 'key_code': ',', 'character': '<', 'arg': None},
"MORETHAN": {'label': '>', 'key_code': '.', 'character': '>', 'arg': None},
"CIRCONFLEX": {'label': '^', 'key_code': '6', 'character': '^', 'arg': None},
"ENTER": {'label': 'enter', 'key_code': 'enter', 'character': '\n', 'arg': None},
"SPACE": {'label': 'space', 'key_code': 'space', 'character': ' ', 'arg': None},
"BACKSPACE": {'label': 'bckspc', 'key_code': 'backspace', 'character': '\x08', 'arg': None},
"TAB": {'label': 'tab', 'key_code': 'tab', 'character': '\t', 'arg': None},
"UNDERSCORE": {'label': 'underscore', 'key_code': '_', 'character': '_', 'arg': None},
"PAGEUP": {'label': 'pageup', 'key_code': 'page_up', 'character': None, 'arg': None},
"PAGEDOWN": {'label': 'pagedwn', 'key_code': 'page_down', 'character': None, 'arg': None},
"LEFTARROW": {'label': 'left', 'key_code': 'left', 'character': None, 'arg': None},
"RIGHTARROW": {'label': 'right', 'key_code': 'right', 'character': None, 'arg': None},
"UPARROW": {'label': 'up', 'key_code': 'up', 'character': None, 'arg': None},
"DOWNARROW": {'label': 'down', 'key_code': 'down', 'character': None, 'arg': None},
"ESCAPE": {'label': 'esc', 'key_code': 'esc', 'character': None, 'arg': None},
"HOME": {'label': 'home', 'key_code': 'home', 'character': None, 'arg': None},
"END": {'label': 'end', 'key_code': 'end', 'character': None, 'arg': None},
"DELETE": {'label': 'del', 'key_code': 'delete', 'character': None, 'arg': None},
"SHIFT": {'label': 'shift', 'key_code': 'shift', 'character': None, 'arg': None},
"RSHIFT": {'label': 'rshift', 'key_code': 'right shift', 'character': None, 'arg': None},
"LSHIFT": {'label': 'lshift', 'key_code': 'left shift', 'character': None, 'arg': None},
"CTRL": {'label': 'ctrl', 'key_code': 'trl', 'character': None, 'arg': None},
"RCTRL": {'label': 'rctrl', 'key_code': 'right ctrl', 'character': None, 'arg': None},
"LCTRL": {'label': 'lctrl', 'key_code': 'ctrl', 'character': None, 'arg': None},
"ALT": {'label': 'alt', 'key_code': 'alt', 'character': None, 'arg': None},
"INSERT": {'label': 'insert', 'key_code': 'insert', 'character': None, 'arg': None},
"WINDOWS": {'label': 'win', 'key_code': 'cmd', 'character': None, 'arg': None},
"STARTMENU": {'label': 'startmenu', 'key_code': 'start', 'character': None, 'arg': None},
"CAPSLOCK": {'label': 'caps', 'key_code': 'caps lock', 'character': None, 'arg': None},
"F1": {'label': 'F1', 'key_code': 'f1', 'character': None, 'arg': None},
"F2": {'label': 'F2', 'key_code': 'f2', 'character': None, 'arg': None},
"F3": {'label': 'F3', 'key_code': 'f3', 'character': None, 'arg': None},
"F4": {'label': 'F4', 'key_code': 'f4', 'character': None, 'arg': None},
"F5": {'label': 'F5', 'key_code': 'f5', 'character': None, 'arg': None},
"F6": {'label': 'F6', 'key_code': 'f6', 'character': None, 'arg': None},
"F7": {'label': 'F7', 'key_code': 'f7', 'character': None, 'arg': None},
"F8": {'label': 'F8', 'key_code': 'f8', 'character': None, 'arg': None},
"F9": {'label': 'F9', 'key_code': 'f9', 'character': None, 'arg': None},
"F10": {'label': 'F10', 'key_code': 'f10', 'character': None, 'arg': None},
"F11": {'label': 'F11', 'key_code': 'f11', 'character': None, 'arg': None},
"F12": {'label': 'F12', 'key_code': 'f12', 'character': None, 'arg': None},
"REPEATMODE": {'label': 'repeat', 'key_code': 'REPEATMODE', 'character': None, 'arg': 0},
"SOUND": {'label': 'snd', 'key_code': 'unknown', 'character': None, 'arg': 8},
"CODESET": {'label': 'code', 'key_code': 'unknown', 'character': None, 'arg': 9},
"MOUSERIGHT5": {'label': 'ms right 5', 'key_code': 'MOUSERIGHT5', 'character': None, 'arg': 2},
"MOUSEUP5": {'label': 'ms up 5', 'key_code': 'MOUSEUP5', 'character': None, 'arg': 3},
"MOUSECLICKLEFT": {'label': 'ms clkleft', 'key_code': 'MOUSECLICKLEFT', 'character': None, 'arg': 4},
"MOUSEDBLCLICKLEFT": {'label': 'ms dblclkleft', 'key_code': 'MOUSEDBLCLICKLEFT', 'character': None, 'arg': 5},
"MOUSECLKHLDLEFT": {'label': 'ms hldleft', 'key_code': 'MOUSECLKHLDLEFT', 'character': None, 'arg': 6},
"MOUSEUPLEFT5": {'label': 'ms leftup 5', 'key_code': 'MOUSEUPLEFT5', 'character': None, 'arg': 7},
"MOUSEDOWNLEFT5": {'label': 'ms leftdown 5', 'key_code': 'MOUSEDOWNLEFT5', 'character': None, 'arg': 8},
"MOUSERELEASEHOLD": {'label': 'ms release', 'key_code': 'MOUSERELEASEHOLD', 'character': None, 'arg': 9},
"MOUSELEFT5": {'label': 'ms left 5', 'key_code': 'MOUSELEFT5', 'character': None, 'arg': 0},
"MOUSEDOWN5": {'label': 'ms down 5', 'key_code': 'MOUSEDOWN5', 'character': None, 'arg': 1},
"MOUSECLICKRIGHT": {'label': 'ms clkright', 'key_code': 'MOUSECLICKRIGHT', 'character': None, 'arg': 2},
"MOUSEDBLCLICKRIGHT": {'label': 'ms dblclkright', 'key_code': 'MOUSEDBLCLICKRIGHT', 'character': None, 'arg': 3},
"MOUSECLKHLDRIGHT": {'label': 'ms hldright', 'key_code': 'MOUSECLKHLDRIGHT', 'character': None, 'arg': 4},
"MOUSEUPRIGHT5": {'label': 'ms rightup 5', 'key_code': 'MOUSEUPRIGHT5', 'character': None, 'arg': 5},
"MOUSEDOWNRIGHT5": {'label': 'ms rightdown 5', 'key_code': 'MOUSEDOWNRIGHT5', 'character': None, 'arg': 6},
"MOUSENORMALMODE": {'label': 'normal mode', 'key_code': 'NORMALMODE', 'character': None, 'arg': 7},
"MOUSEUP40": {'label': 'ms up 40', 'key_code': 'MOUSEUP40', 'character': None, 'arg': 8},
"MOUSEUP250": {'label': 'ms up 250', 'key_code': 'MOUSEUP250', 'character': None, 'arg': 9},
"MOUSEDOWN40": {'label': 'ms down 40', 'key_code': 'MOUSEDOWN40', 'character': None, 'arg': 0},
"MOUSEDOWN250": {'label': 'ms down 250', 'key_code': 'MOUSEDOWN250', 'character': None, 'arg': 1},
"MOUSELEFT40": {'label': 'ms left 40', 'key_code': 'MOUSELEFT40', 'character': None, 'arg': 2},
"MOUSELEFT250": {'label': 'ms left 250', 'key_code': 'MOUSELEFT250', 'character': None, 'arg': 3},
"MOUSERIGHT40": {'label': 'ms right 40', 'key_code': 'MOUSERIGHT40', 'character': None, 'arg': 4},
"MOUSERIGHT250": {'label': 'ms right 250', 'key_code': 'MOUSERIGHT250', 'character': None, 'arg': 5},
"MOUSEUPLEFT40": {'label': 'ms leftup 40', 'key_code': 'MOUSEUPLEFT40', 'character': None, 'arg': 6},
"MOUSEUPLEFT250": {'label': 'ms leftup 250', 'key_code': 'MOUSEUPLEFT250', 'character': None, 'arg': 7},
"MOUSEDOWNLEFT40": {'label': 'ms leftdown 40', 'key_code': 'MOUSEDOWNLEFT40', 'character': None, 'arg': 8},
"MOUSEDOWNLEFT250": {'label': 'ms leftdown 250', 'key_code': 'MOUSEDOWNLEFT250', 'character': None, 'arg': 9},
"MOUSEUPRIGHT40": {'label': 'ms rightup 40', 'key_code': 'MOUSEUPRIGHT40', 'character': None, 'arg': 0},
"MOUSEUPRIGHT250": {'label': 'ms rightup 250', 'key_code': 'MOUSEUPRIGHT250', 'character': None, 'arg': 1},
"MOUSEDOWNRIGHT40": {'label': 'ms rightdown 40', 'key_code': 'MOUSEDOWNRIGHT40', 'character': None, 'arg': 2},
"MOUSEDOWNRIGHT250": {'label': 'ms rightdown 250', 'key_code': 'MOUSEDOWNRIGHT250', 'character': None, 'arg': 3}
}
self.config_file = config_file or os.path.join(user_data_dir, 'config.json')
self.default_config = default_config
self.keystrokemap, self.keystrokes = self.initKeystrokeMap()
self.config = self.read_config()
self.actions = {}
def initKeystrokeMap(self):
keystrokemap = {}
keystrokes = []
for key, data in self.key_data.items():
stroke = KeyStroke(key.upper(), data['label'], data['key_code'], data['character'])
keystrokes.append(stroke)
keystrokemap[key.upper()] = stroke
return keystrokemap, keystrokes
def read_config(self):
if self.config_file and os.path.exists(self.config_file):
try:
with open(self.config_file, "r") as file:
data = json.load(file)
# self.update_keystrokes(data) # Note:cause issue to save configuration
self.convert_types(data)
return data
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
logging.warning(f"Error loading configuration: {e}")
config = self.default_config.copy()
config['fastMorseMode'] = config.get('fastMorseMode', False) # Default to False if not set
return config
def update_keystrokes(self, data):
for key in ['keyone', 'keytwo', 'keythree']:
if key in data and data[key] in self.keystrokemap:
data[key] = self.keystrokemap[data[key]]
def convert_types(self, data):
if 'maxDitTime' in data:
data['maxDitTime'] = float(data['maxDitTime'])
if 'minLetterPause' in data:
data['minLetterPause'] = float(data['minLetterPause'])
if 'fontsizescale' in data:
data['fontsizescale'] = int(data['fontsizescale'])
def save_config(self, config):
try:
with open(self.config_file, "w") as file:
json.dump(config, file, indent=4) # self.config
except Exception as e:
logging.warning(f"Error saving configuration: {e}")
def get_config(self):
return self.config
def initActions(self, window):
actions = {}
# Set up actions for each key based on `key_data`
for key, value in self.key_data.items():
label = value['label']
key_code = value['key_code']
character = value['character']
arg = value['arg']
toggle_action = value.get('toggle_action', False)
if key.startswith('MOUSE'):
# Mouse actions will use ActionLegacy
actions[key.upper()] = lambda item, lbl=label, kc=key_code, char=character, a=arg, win=window: ActionLegacy(item, a, lbl, kc)
else:
# Correctly capture the loop variables using default values in lambda
actions[key.upper()] = lambda item, win=window, lbl=label, kc=key_code, char=character, a=arg, tog=toggle_action: ActionKeyStroke(
{'label': lbl, 'key_code': kc, 'character': char, 'arg': a}, kc, tog, win)
# Define special actions with correct lambda capturing
actions["CHANGELAYOUT"] = lambda item, win=window: ChangeLayoutAction(item, win.changeLayout)
actions["PREDICTION_SELECT"] = lambda item, win=window: PredictionSelectLayoutAction(
item, get_predictions_func=win.getTypeStatePredictions)
# Assuming the action name is stored in item['action'] and matches keys in key_data
actions["KEYSTROKE"] = lambda item, kd=self.key_data, win=window: ActionKeyStroke(
item, kd[item['action'].upper()]['key_code'], win=win)
actions["REPEATMODE"] = lambda item, win=window: RepeatOnAction(item, repeat_on_callback=win.enableRepeatMode)
return actions
class TypeState(pressagio.callback.Callback):
def __init__ (self, abbreviations=None):
self.text = ""
self.predictions = None
pressagioconfig_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "res",
"morsewriter_pressagio.ini")
logging.debug(f"[TypeState] Searching for pressagio config file with name: {pressagioconfig_file}")
pressagioconfig = configparser.ConfigParser()
pressagioconfig.read(pressagioconfig_file)
database = pressagioconfig.get("Database", "database")
logging.debug(f"[TypeState] Searching for database file: {database}")
if pressagioconfig:
try:
self.presage = pressagio.Pressagio(self, pressagioconfig)
logging.debug("[TypeState] Pressagio Initialized successfully")
except Exception as e:
logging.error(f"[TypeState] Pressagio Failed to Initialize with error={e}")
self.abbreviations = abbreviations
self.expanded_text = None
self.keyLength = 0
def past_stream (self):
return self.text
def future_stream (self):
return self.text
def pushchar (self, char):
self.text += char
self.predictions = None
logging.debug(f"Updated TypeState text: {self.text}")
def pushstr (self, str):
self.text += str
self.predictions = None
logging.debug(f"Updated TypeState text: {self.text}")
def popchar (self):
self.text = self.text[:-1]
self.predictions = None
def getpredictions(self):
logging.debug("[TypeState] Fetching predictions for text: {}".format(self.text))
if self.predictions is None:
try:
self.predictions = self.presage.predict()
logging.debug("[TypeState] Predictions fetched: {}".format(self.predictions))
except Exception as e:
logging.error(f"[TypeState] Failed to generate predictions: {str(e)}")
self.predictions = []
return self.predictions
def get_abbreviation(self):
logging.debug("[TypeState] Fetching abbreviation for text: {}".format(self.text))
if self.text is not None:
try:
self.expanded_text, self.keyLength = expand_abbreviation(self.text, self.abbreviations)
logging.debug("[TypeState] Abbreviation fetched: {}".format(self.expanded_text))
except Exception as e:
logging.error(f"[TypeState] Failed to get abbreviations: {str(e)}")
self.expanded_text = None, None
return self.expanded_text, self.keyLength
class KeyListenerThread(QThread):
keyEvent = pyqtSignal(str, bool, int) # Emit key name and press/release status
def __init__(self, configured_keys):
super().__init__()
self.configured_keys = configured_keys # keys in the 'keyboard' library format
self.keep_running = True # Control running of the loop
def run(self):
# Check if the operating system is MacOS
if platform.system() == 'Darwin':
# If it is, only allow modifier keys
allowed_keys = ['shift', 'ctrl', 'alt', 'cmd']
self.configured_keys = [key for key in self.configured_keys if key in allowed_keys]
# Setup key hooks once, outside the loop
for key in self.configured_keys:
keyboard.on_press_key(key, self.on_press, suppress=True)
keyboard.on_release_key(key, self.on_release, suppress=True)
# Minimal loop to keep the thread alive
while self.keep_running:
time.sleep(0.1) # Small sleep to avoid CPU overload
def on_press(self, event):
logging.debug(f"[KeyListenerThread] on_press: {event.name}")
try:
role = self.configured_keys.index(event.name)
self.keyEvent.emit(event.name, True, role)
except Exception as e:
logging.warning(f"[KeyListenerThread] Error handling on_press key event: {e}")
def on_release(self, event):
logging.debug(f"[KeyListenerThread] on_release: {event.name}")
try:
role = self.configured_keys.index(event.name)
self.keyEvent.emit(event.name, False, role)
except Exception as e:
logging.warning(f"[KeyListenerThread] Error handling on_release key event: {e}")
def stop(self):
self.keep_running = False # Signal the loop to stop
keyboard.unhook_all() # Unhook all keys
self.quit() # Quit the thread's event loop if necessary
self.wait() # Wait for the thread to finish
class KeyCombinationListener(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.current_modifiers = 0
self.current_key = 0
logging.debug(f"[KeyCombinationListener __init__]")
def keyPressEvent(self, event):
self.current_modifiers |= int(event.modifiers())
self.current_key = event.key()
if (self.current_modifiers & Qt.CTRL and
self.current_modifiers & Qt.SHIFT and
self.current_key == Qt.Key_P):
self.resetState()
logging.debug("[KeyCombinationListener] \"Ctrl + Shift + P\" detected Escaping Morse Mode")
return True
else:
self.resetState()
return False
def keyReleaseEvent(self, event):
self.resetState()
def resetState(self):
self.current_modifiers = 0
self.current_key = 0
class PressagioCallback(pressagio.callback.Callback):
def __init__(self, buffer):
super().__init__()
self.buffer = buffer
def past_stream(self):
return self.buffer
def future_stream(self):
return ""
class LayoutManager:
def __init__(self, layout_file):
self.layout_file = layout_file
self.layouts = {}
self.active_layout_name = None
self.main_layout_name = None
self.load_layouts()
def load_layouts(self):
"""Loads layout data from a JSON file without assigning actions."""
try:
with open(self.layout_file, "r") as f:
data = json.load(f)
self.layouts = {k: v for k, v in data['layouts'].items()}
self.main_layout_name = data.get('mainlayout')
self.active_layout_name = data.get('mainlayout')
if self.active_layout_name not in self.layouts:
raise ValueError("No valid main layout found in the layout file.")
except FileNotFoundError:
raise Exception(f"Layout file {self.layout_file} not found.")
except json.JSONDecodeError:
raise Exception("Error decoding JSON from the layout file.")
def set_actions(self, actions):
"""Integrates actions with the layout items loaded from the layout file."""
for layout_name, layout in self.layouts.items():
if 'items' in layout:
for item in layout['items']:
action_name = item.get('action')
if action_name in actions:
item['_action'] = actions[action_name](item)
else:
item['_action'] = None
logging.warning(f"No action found for {action_name} in layout {layout_name}")
def set_active(self, layout_name):
"""Sets the active layout by name."""
if layout_name in self.layouts:
self.active_layout_name = layout_name
logging.info(f"Active layout set to {layout_name}")
else:
raise ValueError("Specified layout does not exist.")
def get_active_layout(self):
"""Returns the currently active layout."""
if self.active_layout_name:
return self.layouts[self.active_layout_name]
else:
raise ValueError("No active layout set.")
def moveMouse(x_delta, y_delta):
logging.info(f"moveMouse to {x_delta} {y_delta}")
# current_pos = mouse.get_position()
# new_pos = (current_pos[0] + x_delta, current_pos[1] + y_delta)
mouse.move(x_delta, y_delta, False)
def clickMouse(button='left', action='click'):
logging.info(f"clickMouse to {button} {action}")
btn = mouse.LEFT if button == 'left' else mouse.RIGHT
if action == 'click':
mouse.click(btn)
elif action == 'press':
mouse.press(btn)
elif action == 'release':
mouse.release(btn)
def getPossibleCombos(currentCharacter):
x = ""
for i in currentCharacter:
x += str(i)
possibleactions = []
for action in normalmapping:
if (len(action) >= len(x) and action[:len(x)] == x):
possibleactions.append(action)
logging.debug("possible: %s", str(possibleactions))
class Action (object):
def __init__(self, item):
self.item = item
def getlabel (self):
return self.item.get('label', "")
def perform (self):
pass
class ActionLegacy (Action):
def __init__(self, item, arg, label, key=None):
super(ActionLegacy, self).__init__(item) # Pass required parameters
# Additional initialization for ActionLegacy
self.arg = arg
self.label = label
self.key = key
def getlabel (self):
return self.label
def perform(self):
logging.debug(f"[ActionLegacy] Key to press/release: {self.key}, type: {type(self.key)}")
action_map = {
'MOUSEUP5': lambda: moveMouse(0, -5),
'MOUSEDOWN5': lambda: moveMouse(0, 5),
'MOUSELEFT5': lambda: moveMouse(-5, 0),
'MOUSERIGHT5': lambda: moveMouse(5, 0),
'MOUSEUPLEFT5': lambda: moveMouse(-5, -5),
'MOUSEUPRIGHT5': lambda: moveMouse(5, -5),
'MOUSEDOWNLEFT5': lambda: moveMouse(-5, 5),
'MOUSEDOWNRIGHT5': lambda: moveMouse(5, 5),
'MOUSEUP40': lambda: moveMouse(0, -40),
'MOUSEDOWN40': lambda: moveMouse(0, 40),
'MOUSELEFT40': lambda: moveMouse(-40, 0),
'MOUSERIGHT40': lambda: moveMouse(40, 0),
'MOUSEUPLEFT40': lambda: moveMouse(-40, -40),
'MOUSEUPRIGHT40': lambda: moveMouse(40, -40),
'MOUSEDOWNLEFT40': lambda: moveMouse(-40, 40),
'MOUSEDOWNRIGHT40': lambda: moveMouse(40, 40),
'MOUSEUP250': lambda: moveMouse(0, -250),
'MOUSEDOWN250': lambda: moveMouse(0, 250),
'MOUSELEFT250': lambda: moveMouse(-250, 0),
'MOUSERIGHT250': lambda: moveMouse(250, 0),
'MOUSEUPLEFT250': lambda: moveMouse(-250, -250),
'MOUSEUPRIGHT250': lambda: moveMouse(250, -250),
'MOUSEDOWNLEFT250': lambda: moveMouse(-250, 250),
'MOUSEDOWNRIGHT250': lambda: moveMouse(250, 250),
'MOUSECLICKLEFT': lambda: clickMouse(mouse.LEFT, 'click'),
'MOUSECLICKRIGHT': lambda: clickMouse(mouse.RIGHT, 'click'),
'MOUSECLKHLDLEFT': lambda: clickMouse(mouse.LEFT, 'press'),
'MOUSECLKHLDRIGHT': lambda: clickMouse(mouse.RIGHT, 'press'),
'MOUSERELEASEHOLD': lambda: clickMouse(mouse.LEFT, 'release') # Assumes left button for example
}
# Execute the mapped function based on self.key if exists
if self.key and self.key in action_map:
action_map[self.key]()
else:
logging.debug(f"[ActionLegacy-perform] No action defined for key: {self.key}")
class KeyStroke:
def __init__(self, name, label, key_code, character):
self.name = name
self.label = label
self.key_code = key_code
self.character = character
class ActionKeyStroke(Action):
def __init__(self, item, key, toggle_action=False, window=None):
super(ActionKeyStroke, self).__init__(item)
self.key = key
self.label = item.get('label', item.get('action'))
self.toggle_action = toggle_action
self.window = window
@property
def name(self):
return self.key
@property
def repeaton(self):
return self.window.repeaton # Access dynamically
def set_repeaton(self, repeaton):
self.window.repeaton = repeaton
def getlabel(self):
# Returns the label associated with this action, if any.
return self.label
def perform(self):
logging.debug(f"[ActionKeyStroke] Key to press/release: {self.key}, type: {type(self.key)}")
try:
if self.toggle_action:
current_state = get_keystroke_state(self.key) # Ensure this function is properly defined.
if current_state['down']:
keyboard.release(self.key)
else:
keyboard.press(self.key)
else:
logging.debug(f"[ActionKeyStroke] pressing {self.key}, past Stream {self.window.typestate.past_stream()}")
keyboard.press_and_release(self.key)
# Update typestate based on key action.
if self.window.typestate is not None:
key_char = self.item.get('character') # Assuming 'character' is stored in 'item'
if self.key in ['backspace', 'delete']:
logging.debug(f"[ActionKeyStroke] popchar")
self.window.typestate.popchar()
else:
logging.debug(f"[ActionKeyStroke] pushchar")
self.window.typestate.pushchar(key_char)
abbreviation, keylength = self.window.typestate.get_abbreviation()
if abbreviation is not None:
for _ in range(keylength):
self.window.typestate.popchar()
keyboard.press_and_release('backspace')
for itm in abbreviation:
keyboard.press_and_release(itm)
keyboard.press_and_release('space')
except Exception as e:
logging.error(f"[ActionKeyStroke] Error during key press/release: {e}")
class ChangeLayoutAction(Action):
def __init__(self, item, change_layout_callback):
super().__init__(item)
self.change_layout_callback = change_layout_callback
self.layout_name = item['target']
def perform(self):
# Now call the callback with the stored layout name
if callable(self.change_layout_callback):
self.change_layout_callback(self.layout_name)
else:
raise ValueError("Change Layout callback is not callable")
# def __init__(self, item, window):
# super().__init__(item)
# self.window = window
#
# def perform(self):
# target_layout = self.item.get('target')
# if target_layout and target_layout in self.window.layoutManager.layouts:
# # Use the layout name to set the active layout
# self.window.layoutManager.set_active(target_layout)
# self.codeslayoutview.changeLayoutSignal.emit()
# else:
# raise ValueError(f"Layout '{target_layout}' not found")
class PredictionSelectLayoutAction(Action):
def __init__(self, item, get_predictions_func):
super(PredictionSelectLayoutAction, self).__init__(item)
self.get_predictions_func = get_predictions_func
def getlabel(self):
predictions = self.get_predictions_func()
target = self.item.get('target', -1)
if 0 <= target < len(predictions):
return predictions[target]
return ""
def perform(self):
logging.debug(f"[PredictionSelectLayoutAction] perform")
if typestate is not None:
target = self.item['target']
predictions = typestate.getpredictions()
if target >= 0 and target < len(predictions):
pred = predictions[target]
stripsuffix = ""
for i in range(len(pred)):
idx = len(typestate.text) - i
if idx == 0 or (idx > 0 and typestate.text[idx - 1] in [" ", "\n", "\t", "\r"]):
stripsuffix = typestate.text[idx:]
break
newchars = pred[len(stripsuffix):] + " "
typestate.text = typestate.text[:len(typestate.text)-len(stripsuffix)] + newchars
for char in newchars:
keyboard.press_and_release(char) # This handles normal characters
class RepeatOnAction(Action):
def __init__(self, item, repeat_on_callback):
super(RepeatOnAction, self).__init__(item)
self.repeat_on_callback = repeat_on_callback
def perform(self):
if callable(self.repeat_on_callback):
self.repeat_on_callback()
else:
raise ValueError("Repeat On callback is not callable")
class Window(QDialog):
def __init__(self, layoutManager=None, configManager=None):
super(Window, self).__init__()
self.layoutManager = layoutManager
self.configManager = configManager
self.config = self.configManager.get_config()
self.typestate = None
self.actions = {}
self.keystrokes = []
self.keystrokemap = {}
logging.info(f"Window initialized with layout: {self.layoutManager.main_layout_name}")
self.listenerThread = None
self.currentCharacter = []
self.previousCharacter = []
self.lastKeyDownTime = None
self.endCharacterTimer = None
self.inputDisabled = False
self.codeslayoutview = None
self.fast_morse_mode_timer = None
self.repeat_character_timer = None
self.repeaton = False
self.audioSelector = AudioDeviceSelector()
def load_default_config(self):
return DEFAULT_CONFIG.copy()
def init(self):
self.currentCharacter = []
self.previousCharacter = []
self.repeaton = False
logging.debug("[Window init] Setting active layout to: %s", self.layoutManager.main_layout_name)
self.layoutManager.set_active(self.layoutManager.main_layout_name)
logging.debug("[Window init] Active layout successfully set to: %s", self.layoutManager.active_layout_name)
# Check for specific layout types that may require special handling
if self.layoutManager.main_layout_name == 'typing':
self.abbreviations = load_abbreviations(os.path.join(user_data_dir,"abbreviations_en.txt"))
self.typestate = TypeState(self.abbreviations)
else:
self.typestate = None
logging.debug(f"[Window init] layout that is active is: {self.layoutManager.main_layout_name} ")
self.codeslayoutview = CodesLayoutViewWidget(self.layoutManager.get_active_layout(), self.config)
# I Think this should be what we really need to do - but it's not working.
# self.codeslayoutview = CodesLayoutViewWidget(self.layoutManager.get_active_layout(), self.config, self)
self.codeslayoutview.show()
logging.debug(f"[Window init] Initial visibility status: {self.codeslayoutview.isVisible()}")
def postInit(self):
# Initialize components that depend on actions being available
self.actions = self.configManager.actions
self.keystrokes = self.configManager.keystrokes
self.keystrokemap = self.configManager.keystrokemap
#self.codeslayoutview = CodesLayoutViewWidget(self.layoutManager.get_active_layout(), self.config, self)
self.createIconGroupBox()
self.createActions()
self.createTrayIcon()
self.trayIcon.activated.connect(self.iconActivated)
self.GOButton.clicked.connect(self.goForIt)
self.SaveButton.clicked.connect(self.saveSettings)
self.DeviceButton.clicked.connect(self.changeAudioDevice)
self.withSound.clicked.connect(self.updateAudioProperties)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.iconGroupBox)
self.setLayout(mainLayout)
self.setIcon()
self.trayIcon.show()
self.setWindowTitle("MorseWriter V2.1")
self.resize(400, 300)
def get_configured_keys(self):
key_codes = []
default_keys = {'keyone': 'SPACE', 'keytwo': 'ENTER', 'keythree': 'RIGHT CTRL'}
for key in ['keyone', 'keytwo', 'keythree']:
config_key = self.config.get(key, default_keys[key])
try:
# Ensure keys are fetched in uppercase, which seems to be the format used in keystrokemap
key_code = self.keystrokemap[config_key.upper()].key_code
key_codes.append(key_code)
except KeyError:
logging.error(f"Configured key '{config_key}' not found in keystroke map.")
raise ValueError(f"Configured key '{config_key}' is invalid.")
except AttributeError:
logging.error(f"'KeyStroke' object for '{config_key}' is missing 'key_code' attribute.")
raise
return key_codes
def startKeyListener(self):
key_codes = self.get_configured_keys()
logging.debug(f"[Window startKeyListener] Configured keys: {key_codes}")
if not self.listenerThread:
self.listenerThread = KeyListenerThread(configured_keys=key_codes)
self.listenerThread.keyEvent.connect(self.handle_key_event)
self.listenerThread.start()
def updateAudioProperties(self):
if self.withSound.isChecked():
self.iconComboBoxSoundDit.setEnabled(True)
self.iconComboBoxSoundDah.setEnabled(True)
else:
self.iconComboBoxSoundDit.setEnabled(False)
self.iconComboBoxSoundDah.setEnabled(False)
def changeLayout(self, layout_name):
logging.debug(f"[Window changeLayout] Attempting to change layout to: {layout_name}")
if layout_name in self.layoutManager.layouts:
self.layoutManager.set_active(layout_name)
logging.debug(f"[Window changeLayout] Layout set active: {layout_name}")
if self.codeslayoutview:
self.codeslayoutview.hide()
self.codeslayoutview.deleteLater()
logging.debug("[Window changeLayout] Previous layout view deleted")
self.codeslayoutview = CodesLayoutViewWidget(self.layoutManager.get_active_layout(), self.config)
self.codeslayoutview.show()
logging.debug(f"[Window changeLayout] CodesLayoutViewWidget is visible: {self.codeslayoutview.isVisible()}")
logging.debug(f"[Window changeLayout] Window is visible: {self.isVisible()}")
# Explicitly call show() on both the widget and the window
# self.codeslayoutview.show()
# self.show()
else:
logging.error(f"[Window changeLayout] Layout change failed: {layout_name} not found.")
def getTypeStatePredictions(self):
logging.debug(f"[Window] getTypeStatePredictions")
if self.typestate:
return self.typestate.getpredictions()
return []
def collect_config(self):
config = {
'keylen': self.keySelectionRadioOneKey.isChecked() and 1 or self.keySelectionRadioTwoKey.isChecked() and 2 or 3,
'keyone': self.iconComboBoxKeyOne.itemData(self.iconComboBoxKeyOne.currentIndex()),
'keytwo': self.iconComboBoxKeyTwo.itemData(self.iconComboBoxKeyTwo.currentIndex()),
'keythree': self.iconComboBoxKeyThree.itemData(self.iconComboBoxKeyThree.currentIndex()),
'maxDitTime': float(self.maxDitTimeEdit.text()),
'minLetterPause': float(self.minLetterPauseEdit.text()),
'withsound': self.withSound.isChecked(),
'SoundDit': self.iconComboBoxSoundDit.itemData(self.iconComboBoxSoundDit.currentIndex()),
'SoundDah': self.iconComboBoxSoundDah.itemData(self.iconComboBoxSoundDah.currentIndex()),
'SoundTyping': self.iconComboBoxSoundTyping.itemData(self.iconComboBoxSoundTyping.currentIndex()),
'debug': self.withDebug.isChecked(),
'off': False,
'fontsizescale': float(self.fontSizeScaleEdit.text()),
'upperchars': self.upperCharsCheck.isChecked(),
'autostart': self.autostartCheckbox.isChecked(),
'winxaxis': "left" if self.keyWinPosXLeftRadio.isChecked() else "right",
'winyaxis': "top" if self.keyWinPosYTopRadio.isChecked() else "bottom",
'winposx': self.keyWinPosXEdit.text(),
'winposy': self.keyWinPosYEdit.text(),
'fastMorseMode': self.fastMorseModeCheckbox.isChecked() if self.keySelectionRadioOneKey.isChecked() is False else False,
}
return config
def goForIt(self):
key_one = self.iconComboBoxKeyOne.currentData()
key_two = self.iconComboBoxKeyTwo.currentData()
key_three = self.iconComboBoxKeyThree.currentData()
warn_user = (self.keySelectionRadioTwoKey.isChecked() and key_one == key_two) or (self.keySelectionRadioThreeKey.isChecked() and (key_one == key_two or key_one == key_three or key_two == key_three))
if warn_user:
QMessageBox.warning(self, "MorseWriter",
"Input keys can not be similar, please make sure using different keys.")
return