Skip to content

Commit 1b62ebf

Browse files
authored
Add files via upload
1 parent 8bb2dba commit 1b62ebf

2 files changed

Lines changed: 80 additions & 40 deletions

File tree

index.html

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,37 @@ <h2>輸入法模式:</h2>
839839

840840
// keydown 事件處理
841841
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,除非後面有組合鍵
870+
return;
871+
}
872+
842873
const virtualKeyCode = getKeyCode(event);
843874
if (virtualKeyCode) {
844875
const keyElement = document.querySelector(`.key[data-code="${virtualKeyCode}"]`);
@@ -850,7 +881,7 @@ <h2>輸入法模式:</h2>
850881
// 處理 Shift + < 或 Shift + > 的翻頁功能
851882
if (event.shiftKey && (event.key === '<' || event.key === '>')) {
852883
if (!isEnglishMode && currentCandidates.length > candidatesPerPage) {
853-
event.preventDefault();
884+
event.preventDefault(); // 阻止預設的 < > 輸入
854885
if (event.key === '<') {
855886
handlePageChange('previous');
856887
} else if (event.key === '>') {
@@ -860,33 +891,21 @@ <h2>輸入法模式:</h2>
860891
}
861892
}
862893

863-
if (event.key === 'Shift') {
864-
if (!isShiftDown) {
865-
isShiftDown = true;
866-
clearTimeout(shiftTimer);
867-
shiftTimer = setTimeout(() => {
868-
isLongPressActive = true;
869-
updateKeyboardDisplay();
870-
}, SHIFT_HOLD_THRESHOLD);
871-
}
872-
return;
873-
}
874-
875-
// 在中文模式下,處理長按 Shift 後的符號鍵
894+
// 處理中文模式下的 Shift 符號鍵
876895
if (!isEnglishMode && event.shiftKey) {
877896
const code = getKeyCode(event);
878897
const outputChar = keyname_map_chinese_shifted[code];
879898
if (outputChar) {
880-
event.preventDefault();
899+
event.preventDefault(); // 阻止預設的數字/符號輸入
881900
insertTextAtCursor(textOutput, outputChar);
882901
return;
883902
}
884903
}
885-
886-
// 處理一般輸入
904+
905+
// 處理一般的按鍵輸入 (中文模式或英文模式)
887906
const code = getKeyCode(event);
888907
if (code && code !== 'shift') {
889-
event.preventDefault();
908+
event.preventDefault(); // 阻止預設的字元輸入,交給輸入法處理
890909
if (isEnglishMode) {
891910
handleEnglishInput(event.key);
892911
} else {
@@ -913,12 +932,13 @@ <h2>輸入法模式:</h2>
913932
shiftTimer = null;
914933
}
915934

935+
// 只有在長按未啟動時才切換模式
916936
if (!isLongPressActive) {
917937
isEnglishMode = !isEnglishMode;
918938
updateModeDisplay();
919939
}
920940

921-
isLongPressActive = false;
941+
isLongPressActive = false; // 重置長按標誌
922942
updateKeyboardDisplay();
923943
}
924944
});
@@ -942,7 +962,7 @@ <h2>輸入法模式:</h2>
942962
// 額外處理 Backspace 鍵清除
943963
searchInput.addEventListener('keydown', (event) => {
944964
if (event.key === 'Backspace') {
945-
event.stopPropagation();
965+
event.stopPropagation(); // 防止輸入法影響 searchInput 的 Backspace
946966
searchInput.value = '';
947967
searchResult.textContent = '查詢結果顯示在此';
948968
}

test.html

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,37 @@ <h2>輸入法模式:</h2>
839839

840840
// keydown 事件處理
841841
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,除非後面有組合鍵
870+
return;
871+
}
872+
842873
const virtualKeyCode = getKeyCode(event);
843874
if (virtualKeyCode) {
844875
const keyElement = document.querySelector(`.key[data-code="${virtualKeyCode}"]`);
@@ -850,7 +881,7 @@ <h2>輸入法模式:</h2>
850881
// 處理 Shift + < 或 Shift + > 的翻頁功能
851882
if (event.shiftKey && (event.key === '<' || event.key === '>')) {
852883
if (!isEnglishMode && currentCandidates.length > candidatesPerPage) {
853-
event.preventDefault();
884+
event.preventDefault(); // 阻止預設的 < > 輸入
854885
if (event.key === '<') {
855886
handlePageChange('previous');
856887
} else if (event.key === '>') {
@@ -860,33 +891,21 @@ <h2>輸入法模式:</h2>
860891
}
861892
}
862893

863-
if (event.key === 'Shift') {
864-
if (!isShiftDown) {
865-
isShiftDown = true;
866-
clearTimeout(shiftTimer);
867-
shiftTimer = setTimeout(() => {
868-
isLongPressActive = true;
869-
updateKeyboardDisplay();
870-
}, SHIFT_HOLD_THRESHOLD);
871-
}
872-
return;
873-
}
874-
875-
// 在中文模式下,處理長按 Shift 後的符號鍵
894+
// 處理中文模式下的 Shift 符號鍵
876895
if (!isEnglishMode && event.shiftKey) {
877896
const code = getKeyCode(event);
878897
const outputChar = keyname_map_chinese_shifted[code];
879898
if (outputChar) {
880-
event.preventDefault();
899+
event.preventDefault(); // 阻止預設的數字/符號輸入
881900
insertTextAtCursor(textOutput, outputChar);
882901
return;
883902
}
884903
}
885-
886-
// 處理一般輸入
904+
905+
// 處理一般的按鍵輸入 (中文模式或英文模式)
887906
const code = getKeyCode(event);
888907
if (code && code !== 'shift') {
889-
event.preventDefault();
908+
event.preventDefault(); // 阻止預設的字元輸入,交給輸入法處理
890909
if (isEnglishMode) {
891910
handleEnglishInput(event.key);
892911
} else {
@@ -913,12 +932,13 @@ <h2>輸入法模式:</h2>
913932
shiftTimer = null;
914933
}
915934

935+
// 只有在長按未啟動時才切換模式
916936
if (!isLongPressActive) {
917937
isEnglishMode = !isEnglishMode;
918938
updateModeDisplay();
919939
}
920940

921-
isLongPressActive = false;
941+
isLongPressActive = false; // 重置長按標誌
922942
updateKeyboardDisplay();
923943
}
924944
});
@@ -942,7 +962,7 @@ <h2>輸入法模式:</h2>
942962
// 額外處理 Backspace 鍵清除
943963
searchInput.addEventListener('keydown', (event) => {
944964
if (event.key === 'Backspace') {
945-
event.stopPropagation();
965+
event.stopPropagation(); // 防止輸入法影響 searchInput 的 Backspace
946966
searchInput.value = '';
947967
searchResult.textContent = '查詢結果顯示在此';
948968
}

0 commit comments

Comments
 (0)