• 2

請問imac鍵盤 數字鍵可以固定半形嗎

從windows筆電轉到MacBook花一段時間適應輸入法,現在換到iMac鍵盤有單獨分出的數字鍵還是偶爾會忘記切換全形半形,希望可以修正這個問題啊!
我最後是用Hammerspoon這個軟體直接映射按鍵代碼
靠AI幫忙寫出程式如下:
-- ====================================================================
-- 半形輸入優化腳本(更新符號映射符合用戶需求)
-- 中文輸入時,數字鍵盤、Shift+英數與Shift+符號輸出半形
-- ====================================================================

local DELETE_KEY_CODE = 51
local RELOAD_INTERVAL = 300 -- 定時重啟秒數

-- 數字鍵盤 keyCode 對應半形字元
local numpadTable = {
[82] = "0", [83] = "1", [84] = "2", [85] = "3",
[86] = "4", [87] = "5", [88] = "6", [89] = "7",
[91] = "8", [92] = "9", [65] = ".", [67] = "*",
[69] = "+", [71] = "DELETE_KEY",
[75] = "/", [76] = "\r", [78] = "-", [81] = "=",
}

-- Shift+上排數字鍵 半形符號對應
local shiftNumSymbol = {
[18] = "!", [19] = "@", [20] = "#", [21] = "$", [23] = "%",
[22] = "^", [26] = "&", [28] = "*", [25] = "(", [29] = ")"
}

-- Shift+英文字母keyCode to ASCII半形大寫字母
local keyCodeToAscii = {
[0]="A",[11]="B",[8]="C",[2]="D",[14]="E",[3]="F",[5]="G",[4]="H",[34]="I",
[38]="J",[40]="K",[37]="L",[46]="M",[45]="N",[31]="O",[35]="P",[12]="Q",
[15]="R",[1]="S",[17]="T",[32]="U",[9]="V",[13]="W",[7]="X",[16]="Y",[6]="Z"
}

-- Shift+特殊符號 keyCode to 半形符號 (依用戶需求更新)
local shiftSymbolMap = {
[43] = "<", -- 逗號 ','
[44] = "?", -- 斜線 '/'
[27] = "_", -- 減號 '-'
[47] = ">", -- 句號 '.'
[41] = ":", -- 分號 ';'
[39] = "\"", -- 單引號 '''
[33] = "{", -- 左中括號 '[]'
[30] = "}", -- 右中括號 ']'
[42] = "|", -- 反斜線 '\'
[24] = "+", -- 等號 '='
}

-- 中文輸入判斷
local chineseKeywords = {
"Bopomofo", "Zhuyin", "Wubi", "Pinyin", "Cangjie", "Chinese"
}
local function isChineseInput()
local sid = hs.keycodes.currentSourceID() or ""
for _, kw in ipairs(chineseKeywords) do
if sid:find(kw) then return true end
end
return false
end

-- 核心事件監聽器
remapAllEvent = hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(event)
if not isChineseInput() then return false end

local code = event:getKeyCode()
local flags = event:getFlags()
local isShift = flags.shift
local handled = false

-- 數字鍵盤半形輸出
local numpadChar = numpadTable[code]
if numpadChar then
if numpadChar == "DELETE_KEY" then
hs.eventtap.event.newKeyEvent({}, DELETE_KEY_CODE, true):post()
hs.eventtap.event.newKeyEvent({}, DELETE_KEY_CODE, false):post()
elseif numpadChar ~= "" then
hs.eventtap.keyStrokes(numpadChar)
end
handled = true
end

-- Shift + 英文字母 半形大寫
if isShift and keyCodeToAscii[code] then
hs.eventtap.keyStrokes(keyCodeToAscii[code])
handled = true
end

-- Shift + 上排數字 半形符號
if isShift and shiftNumSymbol[code] then
hs.eventtap.keyStrokes(shiftNumSymbol[code])
handled = true
end

-- Shift + 特殊符號 半形符號輸出
if isShift and shiftSymbolMap[code] then
hs.eventtap.keyStrokes(shiftSymbolMap[code])
handled = true
end

if handled then return true end
return false
end)
remapAllEvent:start()

-- 定時保活重啟
local function restartRemap(reason)
remapAllEvent:stop()
remapAllEvent:start()
hs.notify.show("Remap 重啟通知", "", reason .. ":事件監聽器已重啟")
end
hs.timer.doEvery(RELOAD_INTERVAL, function()
restartRemap("Timer")
end)

-- 鎖屏/喚醒自動重啟
local wakeWatcher = hs.caffeinate.watcher.new(function(eventType)
if eventType == hs.caffeinate.watcher.screenDidWake or eventType == hs.caffeinate.watcher.systemDidWake then
hs.timer.doAfter(1, function() restartRemap("Wakeup") end)
end
end)
wakeWatcher:start()

-- 系統保活避免深度休眠
hs.caffeinate.set("systemIdle")

-- 啟動提示
hs.timer.doAfter(1, function()
hs.notify.show("Hammerspoon", "", "半形輸入優化腳本已啟動")
end)
  • 2
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?