@@ -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
@@ -839,34 +839,8 @@ <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,除非後面有組合鍵
842+ // 允許 Ctrl/Meta 鍵組合使用
843+ if ( event . ctrlKey || event . metaKey ) {
870844 return ;
871845 }
872846
@@ -881,7 +855,7 @@ <h2>輸入法模式:</h2>
881855 // 處理 Shift + < 或 Shift + > 的翻頁功能
882856 if ( event . shiftKey && ( event . key === '<' || event . key === '>' ) ) {
883857 if ( ! isEnglishMode && currentCandidates . length > candidatesPerPage ) {
884- event . preventDefault ( ) ; // 阻止預設的 < > 輸入
858+ event . preventDefault ( ) ;
885859 if ( event . key === '<' ) {
886860 handlePageChange ( 'previous' ) ;
887861 } else if ( event . key === '>' ) {
@@ -891,21 +865,44 @@ <h2>輸入法模式:</h2>
891865 }
892866 }
893867
894- // 處理中文模式下的 Shift 符號鍵
868+ if ( event . key === 'Shift' ) {
869+ if ( ! isShiftDown ) {
870+ isShiftDown = true ;
871+ clearTimeout ( shiftTimer ) ;
872+ shiftTimer = setTimeout ( ( ) => {
873+ isLongPressActive = true ;
874+ updateKeyboardDisplay ( ) ;
875+ } , SHIFT_HOLD_THRESHOLD ) ;
876+ }
877+ return ;
878+ }
879+
880+ // 在中文模式下,處理長按 Shift 後的符號鍵
895881 if ( ! isEnglishMode && event . shiftKey ) {
896882 const code = getKeyCode ( event ) ;
897883 const outputChar = keyname_map_chinese_shifted [ code ] ;
898884 if ( outputChar ) {
899- event . preventDefault ( ) ; // 阻止預設的數字/符號輸入
885+ event . preventDefault ( ) ;
900886 insertTextAtCursor ( textOutput , outputChar ) ;
901887 return ;
902888 }
903889 }
904890
905- // 處理一般的按鍵輸入 (中文模式或英文模式)
891+ // 處理實體鍵盤的刪除鍵
892+ if ( event . key === 'Backspace' ) {
893+ if ( currentCode . length > 0 ) {
894+ event . preventDefault ( ) ;
895+ currentCode = currentCode . slice ( 0 , - 1 ) ;
896+ updateStateAndDisplay ( ) ;
897+ } else {
898+ return ; // 允許瀏覽器預設的刪除行為
899+ }
900+ }
901+
902+ // 處理一般輸入
906903 const code = getKeyCode ( event ) ;
907904 if ( code && code !== 'shift' ) {
908- event . preventDefault ( ) ; // 阻止預設的字元輸入,交給輸入法處理
905+ event . preventDefault ( ) ;
909906 if ( isEnglishMode ) {
910907 handleEnglishInput ( event . key ) ;
911908 } else {
@@ -932,13 +929,12 @@ <h2>輸入法模式:</h2>
932929 shiftTimer = null ;
933930 }
934931
935- // 只有在長按未啟動時才切換模式
936932 if ( ! isLongPressActive ) {
937933 isEnglishMode = ! isEnglishMode ;
938934 updateModeDisplay ( ) ;
939935 }
940936
941- isLongPressActive = false ; // 重置長按標誌
937+ isLongPressActive = false ;
942938 updateKeyboardDisplay ( ) ;
943939 }
944940 } ) ;
@@ -949,7 +945,20 @@ <h2>輸入法模式:</h2>
949945 if ( charToSearch . length === 1 ) {
950946 const codes = currentReverseCodemap [ charToSearch ] ;
951947 if ( codes && codes . length > 0 ) {
952- const formattedCodes = codes . map ( code => formatCode ( code ) ) ;
948+ const formattedCodes = codes . map ( ( code , index ) => {
949+ let suffix = '' ;
950+ if ( codes . length > 1 ) {
951+ if ( index < 9 ) {
952+ suffix = ( index + 1 ) . toString ( ) ;
953+ } else if ( index === 9 ) {
954+ suffix = '0' ;
955+ } else {
956+ suffix = '>' ;
957+ }
958+ }
959+ return formatCode ( code ) + suffix ;
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