Cube 9 months ago
commit
6b1e73d06c
3 changed files with 43 additions and 0 deletions
  1. 36
    0
      CARDPUTER CircuitPython/keyboard.py
  2. 5
    0
      CARDPUTER CircuitPython/test.py
  3. 2
    0
      README.md

+ 36
- 0
CARDPUTER CircuitPython/keyboard.py View File

@@ -0,0 +1,36 @@
1
+import sys
2
+
3
+class Keyboard:
4
+    def __init__(self):
5
+        # the number row starts with 3 and ends with the number, e.g. key number 5 is 0x35
6
+        self.number_row=["0x30","0x31","0x32","0x33","0x34","0x35","0x36","0x37","0x38","0x39"]
7
+        self.shift_number_row={"0x21":"!","0x40":"@","0x23":"#","0x24":"$","0x25":"%","0x5e":"^","0x26":"&","0x2a":"*","0x28":"(","0x29":")"}
8
+        self.lowercase={
9
+            "0x71":"q", "0x77":"w", "0x65":"e", "0x72":"r", "0x74":"t", "0x79":"y", "0x75":"u", "0x69":"i", "0x6f":"o", "0x70":"p",
10
+            "0x61":"a", "0x73":"s", "0x64":"d", "0x66":"f", "0x67":"g", "0x68":"h", "0x6a":"j", "0x6b":"k", "0x6c":"l",
11
+            "0x7a":"z", "0x78":"x", "0x63":"c", "0x76":"v", "0x62":"b", "0x6e":"n", "0x6d":"m"
12
+        }
13
+        self.uppercase={
14
+            "0x51":"Q", "0x57":"W", "0x45":"E", "0x52":"R", "0x54":"T", "0x59":"Y", "0x55":"U", "0x49":"I", "0x4f":"O", "0x50":"P",
15
+            "0x41":"A", "0x53":"S", "0x44":"D", "0x46":"F", "0x47":"G", "0x48":"H", "0x4a":"J", "0x4b":"K", "0x4c":"L",
16
+            "0x5a":"Z", "0x58":"X", "0x43":"C", "0x56":"V", "0x42":"B", "0x4e":"N", "0x4d":"M"
17
+        }
18
+
19
+    def scan(self):
20
+        code=hex(ord(sys.stdin.read(1)))
21
+        #printing the code can be useful for debugging purposes, to check that the codes have been logged correctly if something unexpected is happening
22
+        #print(code)
23
+        key = None
24
+        if code in self.number_row:
25
+            key = str(self.number_row.index(code))
26
+        elif code in self.shift_number_row:
27
+            key = self.shift_number_row[code]
28
+        elif code in self.lowercase:
29
+            key = self.lowercase[code]
30
+        elif code in self.uppercase:
31
+            key = self.uppercase[code]
32
+            
33
+        if key:
34
+            return key
35
+        else:
36
+            return "UNKNOWN "+code

+ 5
- 0
CARDPUTER CircuitPython/test.py View File

@@ -0,0 +1,5 @@
1
+from keyboard import Keyboard
2
+
3
+keyb=Keyboard()
4
+while True:
5
+    print(keyb.scan(), end="")

+ 2
- 0
README.md View File

@@ -0,0 +1,2 @@
1
+# What?
2
+Good question I will answer it some other time