1 | #!/usr/bin/python |
---|
2 | import os |
---|
3 | import time |
---|
4 | |
---|
5 | from rgbkbd.core import Monochrome, KeyboardMode |
---|
6 | from rgbkbd.color import Colors |
---|
7 | from rgbkbd.animation import RandomAnimation |
---|
8 | |
---|
9 | |
---|
10 | class SecretNotesMode(KeyboardMode): |
---|
11 | """A KeyboardMode that will unbind all keys so the OS does not see what is |
---|
12 | typed, but it saves what is typed to a file in the user's home directory |
---|
13 | called .secret-<unixepochtime>. In other words, this gives a discrete |
---|
14 | note-taking application. |
---|
15 | """ |
---|
16 | # A mapping to give more readable output for the note taking files |
---|
17 | key_maps = { |
---|
18 | "common": { # same shift or no shift |
---|
19 | "enter": "\n", |
---|
20 | "numenter": "\n", |
---|
21 | "space": " ", |
---|
22 | "tab": "\t", |
---|
23 | "bspace": "\b", |
---|
24 | }, |
---|
25 | False: { # no shift |
---|
26 | "bslash": "\\", |
---|
27 | "colon": ";", |
---|
28 | "comma": ",", |
---|
29 | "dot": ".", |
---|
30 | "equal": "=", |
---|
31 | "grave": "`", |
---|
32 | "lbrace": "[", |
---|
33 | "minus": "-", |
---|
34 | "quote": "'", |
---|
35 | "rbrace": "]", |
---|
36 | "slash": "/", |
---|
37 | }, |
---|
38 | True: { # shift |
---|
39 | "bslash": "|", |
---|
40 | "colon": ":", |
---|
41 | "comma": "<", |
---|
42 | "dot": ">", |
---|
43 | "equal": "+", |
---|
44 | "grave": "~", |
---|
45 | "lbrace": "{", |
---|
46 | "minus": "_", |
---|
47 | "quote": "\"", |
---|
48 | "rbrace": "}", |
---|
49 | "slash": "?", |
---|
50 | }, |
---|
51 | } |
---|
52 | for c in "abcdefghijklmnopqrstuvwxyz": |
---|
53 | key_maps[False][c] = c |
---|
54 | key_maps[True][c] = c.upper() |
---|
55 | for n, c in zip("1234567890", "!@#$%^&*()"): |
---|
56 | key_maps[False][n] = n |
---|
57 | key_maps[True][n] = c |
---|
58 | |
---|
59 | def __init__(self, manager, keyboard): |
---|
60 | super(SecretNotesMode, self).__init__(manager, keyboard, |
---|
61 | static_lighting=Monochrome(keyboard, color=Colors.BLACK), |
---|
62 | animations=[ |
---|
63 | RandomAnimation(foreground=Colors.GREEN, background=Colors.BLACK, keyboard=keyboard), |
---|
64 | ]) |
---|
65 | self.out = None |
---|
66 | self.state = { |
---|
67 | "shift": False, |
---|
68 | } |
---|
69 | |
---|
70 | def start(self, previous_mode=None): |
---|
71 | """Initialize the keyboard mode""" |
---|
72 | super(SecretNotesMode, self).start() |
---|
73 | # disable all keys so the OS doesn't see the keypresses |
---|
74 | self.keyboard.unbind() |
---|
75 | filename = os.path.expanduser("~/.secret-%s" % int(time.time())) |
---|
76 | self.out = open(filename, 'a') |
---|
77 | |
---|
78 | def event(self, key, state): |
---|
79 | """Handle key events""" |
---|
80 | if key in ["lshift", "rshift"]: |
---|
81 | self.state["shift"] = (state == '+') |
---|
82 | elif state == '+': |
---|
83 | # Translate key presses into characters to output. |
---|
84 | try: |
---|
85 | value = self.key_maps["common"][key] |
---|
86 | except KeyError: |
---|
87 | value = self.key_maps[self.state["shift"]].get(key, "<%s>" % key) |
---|
88 | self.out.write(value) |
---|
89 | self.out.flush() |
---|