From 03d0b73b54edbcc9c9bf7d16b7bd89e335870b2d Mon Sep 17 00:00:00 2001 From: Gol3vka Date: Wed, 21 May 2025 15:49:59 +0800 Subject: [PATCH] Fix Issue #60: add rawcode to key mapping for macOS --- extern.go | 7 +- hook.go | 7 ++ hook_test.go | 7 +- tables.go | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+), 2 deletions(-) diff --git a/extern.go b/extern.go index 0e12a83..da582dd 100644 --- a/extern.go +++ b/extern.go @@ -8,6 +8,7 @@ import "C" import ( "log" + "runtime" "time" "encoding/json" @@ -25,7 +26,11 @@ func go_send(s *C.char) { if out.Keychar != CharUndefined { lck.Lock() - raw2key[out.Rawcode] = string([]rune{out.Keychar}) + if runtime.GOOS == "darwin" { + rawToKeyDarwin[out.Rawcode] = string([]rune{out.Keychar}) + } else { + raw2key[out.Rawcode] = string([]rune{out.Keychar}) + } lck.Unlock() } diff --git a/hook.go b/hook.go index f30e1f2..79beab3 100644 --- a/hook.go +++ b/hook.go @@ -25,6 +25,7 @@ import "C" import ( "fmt" + "runtime" "sync" "time" "unsafe" @@ -224,11 +225,17 @@ func RawcodetoKeychar(r uint16) string { lck.RLock() defer lck.RUnlock() + if runtime.GOOS == "darwin" { + return rawToKeyDarwin[r] + } return raw2key[r] } // KeychartoRawcode key char to rawcode func KeychartoRawcode(kc string) uint16 { + if runtime.GOOS == "darwin" { + return keyToRawDarwin[kc] + } return keytoraw[kc] } diff --git a/hook_test.go b/hook_test.go index d9540d5..0488611 100644 --- a/hook_test.go +++ b/hook_test.go @@ -2,6 +2,7 @@ package hook import ( "fmt" + "runtime" "testing" "github.com/vcaesar/tt" @@ -16,7 +17,11 @@ func TestAdd(t *testing.T) { func TestKey(t *testing.T) { k := RawcodetoKeychar(0) - tt.Equal(t, "error", k) + if runtime.GOOS == "darwin" { + tt.Equal(t, "a", k) + } else { + tt.Equal(t, "error", k) + } r := KeychartoRawcode("error") tt.Equal(t, 0, r) diff --git a/tables.go b/tables.go index 0778dcd..ab6d1a7 100644 --- a/tables.go +++ b/tables.go @@ -351,4 +351,214 @@ var ( "toggle touchpad": 255, "hyper": 65517, } + + rawToKeyDarwin = map[uint16]string{ //https://ritter.ist.psu.edu/projects/RUI/macosx/rui.c + 0: "a", + 1: "s", + 2: "d", + 3: "f", + 4: "h", + 5: "g", + 6: "z", + 7: "x", + 8: "c", + 9: "v", + 11: "b", + 12: "q", + 13: "w", + 14: "e", + 15: "r", + 16: "y", + 17: "t", + 18: "1", + 19: "2", + 20: "3", + 21: "4", + 22: "6", + 23: "5", + 24: "equal sign", + 25: "9", + 26: "7", + 27: "dash", + 28: "8", + 29: "0", + 30: "close bracket / å", + 31: "o", + 32: "u", + 33: "open bracket", + 34: "i", + 35: "p", + 36: "enter", + 37: "l", + 38: "j", + 39: "single quote / ø / ä", + 40: "k", + 41: "semi-colon / ñ", + 42: "back slash", + 43: "comma", + 44: "forward slash / ç", + 45: "n", + 46: "m", + 47: "period", + 48: "tab", + 49: "spacebar", + 50: "`", + 51: "backspace", + 53: "escape", + 54: "apps", // right command + 55: "l-super", // left command + 56: "shift", + 57: "caps lock", + 58: "alt", // left option + 59: "control", + 61: "r-alt", // undefined in `keytoraw` map, right option + 65: "decimal point", + 67: "multiply", + 69: "add", + 71: "clear", + 75: "divide", + 76: "numpad enter", // undefined in `keytoraw` map + 78: "subtract", + 82: "numpad 0", + 83: "numpad 1", + 84: "numpad 2", + 85: "numpad 3", + 86: "numpad 4", + 87: "numpad 5", + 88: "numpad 6", + 89: "numpad 7", + 91: "numpad 8", + 92: "numpad 9", + 96: "f5", + 97: "f6", + 98: "f7", + 99: "f3", + 100: "f8", + 101: "f9", + 103: "f11", + 105: "f13", + 107: "f14", + 109: "f10", + 111: "f12", + 113: "f15", + 114: "insert", + 115: "home", + 116: "page up", + 117: "delete", + 118: "f4", + 119: "end", + 120: "f2", + 121: "page down", + 122: "f1", + 123: "left arrow", + 124: "right arrow", + 125: "down arrow", + 126: "up arrow", + 179: "fn", // undefined in `keytoraw` map + } + + keyToRawDarwin = map[string]uint16{ + "a": 0, + "s": 1, + "d": 2, + "f": 3, + "h": 4, + "g": 5, + "z": 6, + "x": 7, + "c": 8, + "v": 9, + "b": 11, + "q": 12, + "w": 13, + "e": 14, + "r": 15, + "y": 16, + "t": 17, + "1": 18, + "2": 19, + "3": 20, + "4": 21, + "6": 22, + "5": 23, + "equal sign": 24, + "9": 25, + "7": 26, + "dash": 27, + "8": 28, + "0": 29, + "close bracket / å": 30, + "o": 31, + "u": 32, + "open bracket": 33, + "i": 34, + "p": 35, + "enter": 36, + "l": 37, + "j": 38, + "single quote / ø / ä": 39, + "k": 40, + "semi-colon / ñ": 41, + "back slash": 42, + "comma": 43, + "forward slash / ç": 44, + "n": 45, + "m": 46, + "period": 47, + "tab": 48, + "spacebar": 49, + "`": 50, + "backspace": 51, + "escape": 53, + "apps": 54, // right command + "l-super": 55, // left command + "shift": 56, + "caps lock": 57, + "alt": 58, // left option + "control": 59, + "r-alt": 61, // undefined in `keytoraw` map, right option + "decimal point": 65, + "multiply": 67, + "add": 69, + "clear": 71, + "divide": 75, + "numpad enter": 76, // undefined in `keytoraw` map + "subtract": 78, + "numpad 0": 82, + "numpad 1": 83, + "numpad 2": 84, + "numpad 3": 85, + "numpad 4": 86, + "numpad 5": 87, + "numpad 6": 88, + "numpad 7": 89, + "numpad 8": 91, + "numpad 9": 92, + "f5": 96, + "f6": 97, + "f7": 98, + "f3": 99, + "f8": 100, + "f9": 101, + "f11": 103, + "f13": 105, + "f14": 107, + "f10": 109, + "f12": 111, + "f15": 113, + "insert": 114, + "home": 115, + "page up": 116, + "delete": 117, + "f4": 118, + "end": 119, + "f2": 120, + "page down": 121, + "f1": 122, + "left arrow": 123, + "right arrow": 124, + "down arrow": 125, + "up arrow": 126, + "fn": 179, // undefined in `keytoraw` map + } )