Skip to content

Commit 84651b0

Browse files
authored
Add files via upload
1 parent 1b62ebf commit 84651b0

1 file changed

Lines changed: 49 additions & 40 deletions

File tree

test.html

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,10 @@ <h2>輸入法模式:</h2>
432432
// 中文模式下,長按 Shift 顯示的對應字元(全數改為半形)
433433
const keyname_map_chinese_shifted = {
434434
'1': '!', '2': '@', '3': '#', '4': '$', '5': '%', '6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
435-
'q': 'Q', 'w': 'W', 'e': 'E', 'r': 'R', 't': 'T', 'y': 'Y', 'u': 'U', 'i': 'I', 'o': 'O', 'p': 'P',
436-
'a': 'A', 's': 'S', 'd': 'D', 'f': 'F', 'g': 'G', 'h': 'H', 'j': 'J', 'k': 'K', 'l': 'L',
435+
'q': 'q', 'w': 'w', 'e': 'e', 'r': 'r', 't': 't', 'y': 'y', 'u': 'u', 'i': 'i', 'o': 'o', 'p': 'p',
436+
'a': 'a', 's': 's', 'd': 'd', 'f': 'f', 'g': 'G', 'h': 'H', 'j': 'J', 'k': 'K', 'l': 'L',
437437
';': ':', "'": '"',
438-
'z': 'Z', 'x': 'X', 'c': 'C', 'v': 'V', 'b': 'B', 'n': 'N', 'm': 'M',
438+
'z': 'z', 'x': 'x', 'c': 'c', 'v': 'v', 'b': 'b', 'n': 'n', 'm': 'm',
439439
',': '<', '.': '>', '/': '?',
440440
};
441441

@@ -458,6 +458,8 @@ <h2>輸入法模式:</h2>
458458
const modeDisplay = document.getElementById('mode-display');
459459
const modeRadioButtons = document.querySelectorAll('input[name="input_mode"]');
460460

461+
const FULL_WIDTH_NUMBERS = ['①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩'];
462+
461463
const MAX_CODE_LENGTH = 3;
462464
const SHIFT_HOLD_THRESHOLD = 300; // 300毫秒
463465

@@ -839,34 +841,8 @@ <h2>輸入法模式:</h2>
839841

840842
// keydown 事件處理
841843
document.addEventListener('keydown', (event) => {
842-
// 檢查是否是瀏覽器預設的組合鍵,如果不是我們處理的,則不阻止其預設行為
843-
const isCtrl = event.ctrlKey;
844-
const isCmd = event.metaKey; // For Mac
845-
const key = event.key.toLowerCase();
846-
847-
// 處理瀏覽器預設的組合鍵,如 Ctrl+C, Ctrl+V, Ctrl+A 等
848-
if ((isCtrl || isCmd) && ['c', 'v', 'a', 'x', 'z', 's', 'f', 'g'].includes(key)) {
849-
// 這些組合鍵由瀏覽器處理,不阻止其預設行為
850-
// 但是,如果我們之前有設定 isLongPressActive,先清掉
851-
if(isLongPressActive) {
852-
isLongPressActive = false;
853-
updateKeyboardDisplay();
854-
}
855-
// 也不要繼續處理我們輸入法的邏輯
856-
return;
857-
}
858-
859-
// 處理 Shift 相關的邏輯
860-
if (event.key === 'Shift') {
861-
if (!isShiftDown) {
862-
isShiftDown = true;
863-
clearTimeout(shiftTimer);
864-
shiftTimer = setTimeout(() => {
865-
isLongPressActive = true;
866-
updateKeyboardDisplay();
867-
}, SHIFT_HOLD_THRESHOLD);
868-
}
869-
// Shift 本身按下不需preventDefault,除非後面有組合鍵
844+
// 允許 Ctrl/Meta 鍵組合使用
845+
if (event.ctrlKey || event.metaKey) {
870846
return;
871847
}
872848

@@ -881,7 +857,7 @@ <h2>輸入法模式:</h2>
881857
// 處理 Shift + < 或 Shift + > 的翻頁功能
882858
if (event.shiftKey && (event.key === '<' || event.key === '>')) {
883859
if (!isEnglishMode && currentCandidates.length > candidatesPerPage) {
884-
event.preventDefault(); // 阻止預設的 < > 輸入
860+
event.preventDefault();
885861
if (event.key === '<') {
886862
handlePageChange('previous');
887863
} else if (event.key === '>') {
@@ -891,21 +867,44 @@ <h2>輸入法模式:</h2>
891867
}
892868
}
893869

894-
// 處理中文模式下的 Shift 符號鍵
870+
if (event.key === 'Shift') {
871+
if (!isShiftDown) {
872+
isShiftDown = true;
873+
clearTimeout(shiftTimer);
874+
shiftTimer = setTimeout(() => {
875+
isLongPressActive = true;
876+
updateKeyboardDisplay();
877+
}, SHIFT_HOLD_THRESHOLD);
878+
}
879+
return;
880+
}
881+
882+
// 在中文模式下,處理長按 Shift 後的符號鍵
895883
if (!isEnglishMode && event.shiftKey) {
896884
const code = getKeyCode(event);
897885
const outputChar = keyname_map_chinese_shifted[code];
898886
if (outputChar) {
899-
event.preventDefault(); // 阻止預設的數字/符號輸入
887+
event.preventDefault();
900888
insertTextAtCursor(textOutput, outputChar);
901889
return;
902890
}
903891
}
904892

905-
// 處理一般的按鍵輸入 (中文模式或英文模式)
893+
// 處理實體鍵盤的刪除鍵
894+
if (event.key === 'Backspace') {
895+
if (currentCode.length > 0) {
896+
event.preventDefault();
897+
currentCode = currentCode.slice(0, -1);
898+
updateStateAndDisplay();
899+
} else {
900+
return; // 允許瀏覽器預設的刪除行為
901+
}
902+
}
903+
904+
// 處理一般輸入
906905
const code = getKeyCode(event);
907906
if (code && code !== 'shift') {
908-
event.preventDefault(); // 阻止預設的字元輸入,交給輸入法處理
907+
event.preventDefault();
909908
if (isEnglishMode) {
910909
handleEnglishInput(event.key);
911910
} else {
@@ -932,13 +931,12 @@ <h2>輸入法模式:</h2>
932931
shiftTimer = null;
933932
}
934933

935-
// 只有在長按未啟動時才切換模式
936934
if (!isLongPressActive) {
937935
isEnglishMode = !isEnglishMode;
938936
updateModeDisplay();
939937
}
940938

941-
isLongPressActive = false; // 重置長按標誌
939+
isLongPressActive = false;
942940
updateKeyboardDisplay();
943941
}
944942
});
@@ -949,7 +947,18 @@ <h2>輸入法模式:</h2>
949947
if (charToSearch.length === 1) {
950948
const codes = currentReverseCodemap[charToSearch];
951949
if (codes && codes.length > 0) {
952-
const formattedCodes = codes.map(code => formatCode(code));
950+
let formattedCodes = codes.map(formatCode);
951+
952+
if (formattedCodes.length > 1) {
953+
formattedCodes = formattedCodes.map((code, index) => {
954+
if (index < 10) {
955+
return `${code}${FULL_WIDTH_NUMBERS[index]}`;
956+
} else {
957+
return `${code}>`;
958+
}
959+
});
960+
}
961+
953962
searchResult.textContent = `編碼: ${formattedCodes.join(' ')}`;
954963
} else {
955964
searchResult.textContent = '找不到該漢字編碼。';
@@ -962,7 +971,7 @@ <h2>輸入法模式:</h2>
962971
// 額外處理 Backspace 鍵清除
963972
searchInput.addEventListener('keydown', (event) => {
964973
if (event.key === 'Backspace') {
965-
event.stopPropagation(); // 防止輸入法影響 searchInput 的 Backspace
974+
event.stopPropagation();
966975
searchInput.value = '';
967976
searchResult.textContent = '查詢結果顯示在此';
968977
}

0 commit comments

Comments
 (0)