|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Dan Halbert |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +# |
| 23 | + |
| 24 | +""" |
| 25 | +* Author(s): Dan Halbert, Neradoc |
| 26 | +""" |
| 27 | + |
| 28 | +#from .keycode import Keycode |
| 29 | +from adafruit_hid.keycode import Keycode |
| 30 | + |
| 31 | + |
| 32 | +__version__ = "0.0.0-auto.0" |
| 33 | +__repo__ = "https://github.com/Neradoc/Circuitpython_Keyboard_Layouts.git" |
| 34 | + |
| 35 | + |
| 36 | +class KeyboardLayoutMacFR: |
| 37 | + """Map ASCII characters to appropriate keypresses on a FR Mac keyboard. |
| 38 | + Non-ASCII characters and most control characters will raise an exception. |
| 39 | + """ |
| 40 | + |
| 41 | + # The ASCII_TO_KEYCODE bytes object is used as a table to maps ASCII 0-127 |
| 42 | + # to the corresponding # keycode on a US 104-key keyboard. |
| 43 | + # The user should not normally need to use this table, |
| 44 | + # but it is not marked as private. |
| 45 | + # |
| 46 | + # Because the table only goes to 127, we use the top bit of each byte (ox80) to indicate |
| 47 | + # that the shift key should be pressed. So any values 0x{8,9,a,b}* are shifted characters. |
| 48 | + # |
| 49 | + # The Python compiler will concatenate all these bytes literals into a single bytes object. |
| 50 | + # Micropython/CircuitPython will store the resulting bytes constant in flash memory |
| 51 | + # if it's in a .mpy file, so it doesn't use up valuable RAM. |
| 52 | + # |
| 53 | + # \x00 entries have no keyboard key and so won't be sent. |
| 54 | + NUL = b'\x00' |
| 55 | + SHIFT = b'\x02' |
| 56 | + ALTGR = b'\x40' |
| 57 | + ALTSHIFT = b'\x42' |
| 58 | + ASCII_TO_KEYCODE = ( |
| 59 | + NUL+b'\x00' # NUL |
| 60 | + +NUL+b'\x00' # SOH |
| 61 | + +NUL+b'\x00' # STX |
| 62 | + +NUL+b'\x00' # ETX |
| 63 | + +NUL+b'\x00' # EOT |
| 64 | + +NUL+b'\x00' # ENQ |
| 65 | + +NUL+b'\x00' # ACK |
| 66 | + +NUL+b'\x00' # BEL \a |
| 67 | + +NUL+b'\x2a' # BS BACKSPACE \b (called DELETE in the usb.org document) |
| 68 | + +NUL+b'\x2b' # TAB \t |
| 69 | + +NUL+b'\x28' # LF \n (called Return or ENTER in the usb.org document) |
| 70 | + +NUL+b'\x00' # VT \v |
| 71 | + +NUL+b'\x00' # FF \f |
| 72 | + +NUL+b'\x00' # CR \r |
| 73 | + +NUL+b'\x00' # SO |
| 74 | + +NUL+b'\x00' # SI |
| 75 | + +NUL+b'\x00' # DLE |
| 76 | + +NUL+b'\x00' # DC1 |
| 77 | + +NUL+b'\x00' # DC2 |
| 78 | + +NUL+b'\x00' # DC3 |
| 79 | + +NUL+b'\x00' # DC4 |
| 80 | + +NUL+b'\x00' # NAK |
| 81 | + +NUL+b'\x00' # SYN |
| 82 | + +NUL+b'\x00' # ETB |
| 83 | + +NUL+b'\x00' # CAN |
| 84 | + +NUL+b'\x00' # EM |
| 85 | + +NUL+b'\x00' # SUB |
| 86 | + +NUL+b'\x29' # ESC |
| 87 | + +NUL+b'\x00' # FS |
| 88 | + +NUL+b'\x00' # GS |
| 89 | + +NUL+b'\x00' # RS |
| 90 | + +NUL+b'\x00' # US |
| 91 | + |
| 92 | + +NUL+b'\x2c' # ' ' |
| 93 | + +NUL+b'\x25' # ! |
| 94 | + +NUL+b'\x20' # " |
| 95 | + +SHIFT+b'\x64' # # |
| 96 | + +NUL+b'\x30' # $ |
| 97 | + +SHIFT+b'\x34' # % |
| 98 | + +NUL+b'\x1E' # & |
| 99 | + +NUL+b'\x21' # ' |
| 100 | + +NUL+b'\x22' # ( |
| 101 | + +NUL+b'\x2d' # ) |
| 102 | + +SHIFT+b'\x30' # * |
| 103 | + +SHIFT+b'\x38' # + |
| 104 | + +NUL+b'\x10' # , |
| 105 | + +NUL+b'\x2E' # - |
| 106 | + +SHIFT+b'\x36' # . |
| 107 | + +SHIFT+b'\x37' # / |
| 108 | + +SHIFT+b'\x27' # 0 |
| 109 | + +SHIFT+b'\x1e' # 1 |
| 110 | + +SHIFT+b'\x1f' # 2 |
| 111 | + +SHIFT+b'\x20' # 3 |
| 112 | + +SHIFT+b'\x21' # 4 |
| 113 | + +SHIFT+b'\x22' # 5 |
| 114 | + +SHIFT+b'\x23' # 6 |
| 115 | + +SHIFT+b'\x24' # 7 |
| 116 | + +SHIFT+b'\x25' # 8 |
| 117 | + +SHIFT+b'\x26' # 9 |
| 118 | + +NUL+b'\x37' # : |
| 119 | + +NUL+b'\x36' # ; |
| 120 | + +NUL+b'\x35' # < |
| 121 | + +NUL+b'\x38' # = |
| 122 | + +SHIFT+b'\x35' # > |
| 123 | + +SHIFT+b'\x10' # ? |
| 124 | + +NUL+b'\x64' # @ |
| 125 | + +SHIFT+b'\x14' # A |
| 126 | + +SHIFT+b'\x05' # B |
| 127 | + +SHIFT+b'\x06' # C |
| 128 | + +SHIFT+b'\x07' # D |
| 129 | + +SHIFT+b'\x08' # E |
| 130 | + +SHIFT+b'\x09' # F |
| 131 | + +SHIFT+b'\x0a' # G |
| 132 | + +SHIFT+b'\x0b' # H |
| 133 | + +SHIFT+b'\x0c' # I |
| 134 | + +SHIFT+b'\x0d' # J |
| 135 | + +SHIFT+b'\x0e' # K |
| 136 | + +SHIFT+b'\x0f' # L |
| 137 | + +SHIFT+b'\x33' # M |
| 138 | + +SHIFT+b'\x11' # N |
| 139 | + +SHIFT+b'\x12' # O |
| 140 | + +SHIFT+b'\x13' # P |
| 141 | + +SHIFT+b'\x04' # Q |
| 142 | + +SHIFT+b'\x15' # R |
| 143 | + +SHIFT+b'\x16' # S |
| 144 | + +SHIFT+b'\x17' # T |
| 145 | + +SHIFT+b'\x18' # U |
| 146 | + +SHIFT+b'\x19' # V |
| 147 | + +SHIFT+b'\x1d' # W |
| 148 | + +SHIFT+b'\x1b' # X |
| 149 | + +SHIFT+b'\x1c' # Y |
| 150 | + +SHIFT+b'\x1a' # Z |
| 151 | + +ALTSHIFT+b'\x22' # [ |
| 152 | + +ALTSHIFT+b'\x37' # bslash |
| 153 | + +ALTSHIFT+b'\x2d' # ] |
| 154 | + +NUL+b'\x2F' # ^ |
| 155 | + +SHIFT+b'\x2E' # _ |
| 156 | + +NUL+b'\x31' # ` |
| 157 | + +NUL+b'\x14' # a |
| 158 | + +NUL+b'\x05' # b |
| 159 | + +NUL+b'\x06' # c |
| 160 | + +NUL+b'\x07' # d |
| 161 | + +NUL+b'\x08' # e |
| 162 | + +NUL+b'\x09' # f |
| 163 | + +NUL+b'\x0a' # g |
| 164 | + +NUL+b'\x0b' # h |
| 165 | + +NUL+b'\x0c' # i |
| 166 | + +NUL+b'\x0d' # j |
| 167 | + +NUL+b'\x0e' # k |
| 168 | + +NUL+b'\x0f' # l |
| 169 | + +NUL+b'\x33' # m |
| 170 | + +NUL+b'\x11' # n |
| 171 | + +NUL+b'\x12' # o |
| 172 | + +NUL+b'\x13' # p |
| 173 | + +NUL+b'\x04' # q |
| 174 | + +NUL+b'\x15' # r |
| 175 | + +NUL+b'\x16' # s |
| 176 | + +NUL+b'\x17' # t |
| 177 | + +NUL+b'\x18' # u |
| 178 | + +NUL+b'\x19' # v |
| 179 | + +NUL+b'\x1d' # w |
| 180 | + +NUL+b'\x1b' # x |
| 181 | + +NUL+b'\x1c' # y |
| 182 | + +NUL+b'\x1a' # z |
| 183 | + +ALTGR+b'\x22' # { |
| 184 | + +ALTSHIFT+b'\x0f' # | |
| 185 | + +ALTGR+b'\x2d' # } |
| 186 | + +ALTGR+b'\x11' # ~ TODO |
| 187 | + +NUL+b'\x00' # DEL |
| 188 | + ) |
| 189 | + |
| 190 | + def __init__(self, keyboard): |
| 191 | + """Specify the layout for the given keyboard. |
| 192 | +
|
| 193 | + :param keyboard: a Keyboard object. Write characters to this keyboard when requested. |
| 194 | +
|
| 195 | + Example:: |
| 196 | +
|
| 197 | + kbd = Keyboard() |
| 198 | + layout = KeyboardLayoutFR(kbd) |
| 199 | + """ |
| 200 | + |
| 201 | + self.keyboard = keyboard |
| 202 | + |
| 203 | + def write(self, string): |
| 204 | + """Type the string by pressing and releasing keys on my keyboard. |
| 205 | +
|
| 206 | + :param string: A string of ASCII characters. |
| 207 | + :raises ValueError: if any of the characters are not ASCII or have no keycode |
| 208 | + (such as some control characters). |
| 209 | +
|
| 210 | + Example:: |
| 211 | +
|
| 212 | + # Write abc followed by Enter to the keyboard |
| 213 | + layout.write('abc\\n') |
| 214 | + """ |
| 215 | + for char in string: |
| 216 | + (modifier,keycode) = self._char_to_keycode(char) |
| 217 | + # If this is a shifted char, clear the SHIFT flag and press the SHIFT key. |
| 218 | + if modifier & self.SHIFT[0]: |
| 219 | + self.keyboard.press(Keycode.SHIFT) |
| 220 | + if modifier & self.ALTGR[0]: |
| 221 | + self.keyboard.press(Keycode.GUI) |
| 222 | + self.keyboard.press(keycode) |
| 223 | + self.keyboard.release_all() |
| 224 | + |
| 225 | + def keycodes(self, char): |
| 226 | + """Return a tuple of keycodes needed to type the given character. |
| 227 | +
|
| 228 | + :param char: A single ASCII character in a string. |
| 229 | + :type char: str of length one. |
| 230 | + :returns: tuple of Keycode keycodes. |
| 231 | + :raises ValueError: if ``char`` is not ASCII or there is no keycode for it. |
| 232 | +
|
| 233 | + Examples:: |
| 234 | +
|
| 235 | + # Returns (Keycode.TAB,) |
| 236 | + keycodes('\t') |
| 237 | + # Returns (Keycode.A,) |
| 238 | + keycode('a') |
| 239 | + # Returns (Keycode.SHIFT, Keycode.A) |
| 240 | + keycode('A') |
| 241 | + # Raises ValueError because it's a accented e and is not ASCII |
| 242 | + keycode('é') |
| 243 | + """ |
| 244 | + out = [] |
| 245 | + (modifier,keycode) = self._char_to_keycode(char) |
| 246 | + if modifier & self.SHIFT: |
| 247 | + out += [Keycode.SHIFT] |
| 248 | + if modifier & self.ALTGR: |
| 249 | + out += [Keycode.GUI] |
| 250 | + out += [keycode] |
| 251 | + return out |
| 252 | + |
| 253 | + def _char_to_keycode(self, char): |
| 254 | + """Return the HID keycode for the given ASCII character, with the SHIFT_FLAG possibly set. |
| 255 | +
|
| 256 | + If the character requires pressing the Shift key, the SHIFT_FLAG bit is set. |
| 257 | + You must clear this bit before passing the keycode in a USB report. |
| 258 | + """ |
| 259 | + char_val = ord(char)*2 |
| 260 | + if char_val+1 >= len(self.ASCII_TO_KEYCODE): |
| 261 | + raise ValueError("Unknown character.") |
| 262 | + modifier = self.ASCII_TO_KEYCODE[char_val] |
| 263 | + keycode = self.ASCII_TO_KEYCODE[char_val+1] |
| 264 | + if keycode == 0: |
| 265 | + raise ValueError("No keycode available for character.") |
| 266 | + return (modifier,keycode) |
0 commit comments