-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
executable file
·65 lines (58 loc) · 2.17 KB
/
main.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
#!/usr/bin/env python2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
# Author: Fábio André Damas <skkeeper at gmail dot com>
from os import listdir, getcwd
from random import choice
from third_party.evdev import DeviceGroup
from linux_clicky.play_sound import PlaySound
from linux_clicky.detect_keyboards import detect_keyboards
from optparse import OptionParser
from signal import signal, SIGINT
from sys import exit
# Handle CTRL+C
def signal_handler(signal, frame):
print '\033[1;32mCTRL + C Detected. Exiting ...'
print 'Ignore any errors after this message.\033[1;m'
exit(0)
signal(SIGINT, signal_handler)
# Handle arguments
parser = OptionParser()
parser.add_option(
'-v', '--volume', action="store", dest='volume',
help="sets the volume of the clicks, anything above 1 will increase the " +
"volume, and anything less will decrease it. Don't use numbers bigger " +
"than 2")
parser.set_defaults(volume=1)
(options, args) = parser.parse_args()
# Get a list of sound files
sounds = listdir(getcwd() + '/sounds')
sound_tmp = {}
sound_tmp["click"] = []
for sound in sounds:
if sound == 'enter.wav':
sound_tmp["enter"] = sound
elif sound == 'space.wav':
sound_tmp["space"] = sound
else:
sound_tmp["click"].append(sound)
sounds = sound_tmp
# Volume: Negative to lower the volume
volume = str(options.volume)
key_sound_pair = dict()
dev = DeviceGroup(detect_keyboards())
while 1:
event = dev.next_event()
if event is not None:
# print repr(event)
if event.type == "EV_KEY" and event.value == 1:
if event.code.startswith("KEY"):
if event.code == "KEY_ENTER":
filename = getcwd() + '/sounds/' + sounds["enter"]
elif event.code == "KEY_SPACE":
filename = getcwd() + '/sounds/' + sounds["space"]
else:
if event.code not in key_sound_pair:
key_sound_pair[event.code] = choice(sounds["click"])
filename = getcwd() + '/sounds/' +\
key_sound_pair[event.code]
PlaySound(filename, volume).start()