1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JPキーボード for CircuitPython

Last updated at Posted at 2023-02-23

CircuitPythonでJPキーボードを使いたい

CircuitPythonで使えるJPキーボードレイアウトが見つからなかったので、自分で作りました。

これまでArduinoで実現してきたことと基本的には同じです。詳しく知りたい方は次の記事をご覧ください。

keyboard_layout_jp.py
#Map ASCII characters to appropriate keypresses on a Japanese keyboard.

from adafruit_hid.keyboard_layout_base import KeyboardLayoutBase

class KeyboardLayoutJP(KeyboardLayoutBase):

    SHIFT_FLAG = (1<<9)
    
    ASCII_TO_KEYCODE = (
        0x0, #  NUL 0 0x00
        0x0, #  SOH 1 0x01
        0x0, #  STX 2 0x02
        0x0, #  ETX 3 0x03
        0x0, #  EOT 4 0x04
        0x0, #  ENQ 5 0x05
        0x0, #  ACK 6 0x06
        0x0, #  BEL 7 0x07
        0x2A, #  BS 8 0x08
        0x2B, #  TAB 9 0x09
        0x28, #  LF 10 0x0A
        0x0, #  VT 11 0x0B
        0x0, #  FF 12 0x0C
        0x0, #  CR 13 0x0D
        0x0, #  SO 14 0x0E
        0x0, #  SI 15 0x0F
        0x0, #  DEL 16 0x10
        0x0, #  DC1 17 0x11
        0x0, #  DC2 18 0x12
        0x0, #  DC3 19 0x13
        0x0, #  DC4 20 0x14
        0x0, #  NAK 21 0x15
        0x0, #  SYN 22 0x16
        0x0, #  ETB 23 0x17
        0x0, #  CAN 24 0x18
        0x0, #  EM 25 0x19
        0x0, #  SUB 26 0x1A
        0x0, #  ESC 27 0x1B
        0x0, #  FS 28 0x1C
        0x0, #  GS 29 0x1D
        0x0, #  RS 30 0x1E
        0x0, #  US 31 0x1F
        0x2C, #  Space 32 0x20
        0x21E, #  ! 33 0x21
        0x21F, #  " 34 0x22
        0x220, #  # 35 0x23
        0x221, #  $ 36 0x24
        0x222, #  % 37 0x25
        0x223, #  & 38 0x26
        0x224, #  ' 39 0x27
        0x225, #  ( 40 0x28
        0x226, #  ) 41 0x29
        0x234, #  * 42 0x2A
        0x233, #  + 43 0x2B
        0x36, #  , 44 0x2C
        0x2D, #  - 45 0x2D
        0x37, #  . 46 0x2E
        0x38, #  / 47 0x2F
        0x27, #  0 48 0x30
        0x1E, #  1 49 0x31
        0x1F, #  2 50 0x32
        0x20, #  3 51 0x33
        0x21, #  4 52 0x34
        0x22, #  5 53 0x35
        0x23, #  6 54 0x36
        0x24, #  7 55 0x37
        0x25, #  8 56 0x38
        0x26, #  9 57 0x39
        0x34, #  : 58 0x3A
        0x33, #  ; 59 0x3B
        0x236, #  < 60 0x3C
        0x22D, #  = 61 0x3D
        0x237, #  > 62 0x3E
        0x238, #  ? 63 0x3F
        0x2F, #  @ 64 0x40
        0x204, #  A 65 0x41
        0x205, #  B 66 0x42
        0x206, #  C 67 0x43
        0x207, #  D 68 0x44
        0x208, #  E 69 0x45
        0x209, #  F 70 0x46
        0x20A, #  G 71 0x47
        0x20B, #  H 72 0x48
        0x20C, #  I 73 0x49
        0x20D, #  J 74 0x4A
        0x20E, #  K 75 0x4B
        0x20F, #  L 76 0x4C
        0x210, #  M 77 0x4D
        0x211, #  N 78 0x4E
        0x212, #  O 79 0x4F
        0x213, #  P 80 0x50
        0x214, #  Q 81 0x51
        0x215, #  R 82 0x52
        0x216, #  S 83 0x53
        0x217, #  T 84 0x54
        0x218, #  U 85 0x55
        0x219, #  V 86 0x56
        0x21A, #  W 87 0x57
        0x21B, #  X 88 0x58
        0x21C, #  Y 89 0x59
        0x21D, #  Z 90 0x5A
        0x30, #  [ 91 0x5B
        0x89, #  ¥ 92 0x5C
        0x32, #  ] 93 0x5D
        0x2E, #  ^ 94 0x5E
        0x287, #  _ 95 0x5F
        0x22F, #  ` 96 0x60
        0x4, #  a 97 0x61
        0x5, #  b 98 0x62
        0x6, #  c 99 0x63
        0x7, #  d 100 0x64
        0x8, #  e 101 0x65
        0x9, #  f 102 0x66
        0xA, #  g 103 0x67
        0xB, #  h 104 0x68
        0xC, #  i 105 0x69
        0xD, #  j 106 0x6A
        0xE, #  k 107 0x6B
        0xF, #  l 108 0x6C
        0x10, #  m 109 0x6D
        0x11, #  n 110 0x6E
        0x12, #  o 111 0x6F
        0x13, #  p 112 0x70
        0x14, #  q 113 0x71
        0x15, #  r 114 0x72
        0x16, #  s 115 0x73
        0x17, #  t 116 0x74
        0x18, #  u 117 0x75
        0x19, #  v 118 0x76
        0x1A, #  w 119 0x77
        0x1B, #  x 120 0x78
        0x1C, #  y 121 0x79
        0x1D, #  z 122 0x7A
        0x230, #  { 123 0x7B
        0x289, #  | 124 0x7C
        0x232, #  } 125 0x7D
        0x22E, #  ~ 126 0x7E
        0x0, #  DEL 127 0x7F
)

使い方

import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from keyboard_layout_jp import KeyboardLayoutJP

keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutJP(keyboard)

string = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
keyboard_layout.write(string)

"""
keycodes = keyboard_layout.keycodes('\\')
print(list(map(lambda v: hex(v), keycodes)))

keycodes = keyboard_layout.keycodes('_')
print(list(map(lambda v: hex(v), keycodes)))

keycodes = keyboard_layout.keycodes('|')
print(list(map(lambda v: hex(v), keycodes)))
"""

鬼門である円マーク¥(バックスラッシュ\)、アンダーライン_とストローク|を、ちゃんとキー入力できました。

実行時に動的にUSキーボードとJPキーボードを切り替えることもできます。

ESP32S3でしか試していませんが、Raspberry Pi Pico等でも使えるはずです。

以上

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?