@@ -416,7 +416,12 @@ <h2>輸入法模式:</h2>
416416 const searchInput = document . getElementById ( 'search-input' ) ;
417417 const searchResult = document . getElementById ( 'search-result' ) ;
418418 const modeDisplay = document . getElementById ( 'mode-display' ) ;
419- const allKeys = document . querySelectorAll ( '.key' ) ;
419+
420+ const fullWidthMap = {
421+ '0' : '0' , '1' : '1' , '2' : '2' , '3' : '3' , '4' : '4' , '5' : '5' , '6' : '6' , '7' : '7' , '8' : '8' , '9' : '9' ,
422+ 'a' : 'A' , 'b' : 'B' , 'c' : 'C' , 'd' : 'D' , 'e' : 'E' , 'f' : 'F' , 'g' : 'G' , 'h' : 'H' , 'i' : 'I' , 'j' : 'J' , 'k' : 'K' , 'l' : 'L' , 'm' : 'M' , 'n' : 'N' , 'o' : 'O' , 'p' : 'P' , 'q' : 'Q' , 'r' : 'R' , 's' : 'S' , 't' : 'T' , 'u' : 'U' , 'v' : 'V' , 'w' : 'W' , 'x' : 'X' , 'y' : 'Y' , 'z' : 'Z' ,
423+ ';' : ';' , "'" : '〃' , ',' : ',' , '.' : '.' , '/' : '/'
424+ } ;
420425
421426 // --- 載入碼表檔案(已修復) ---
422427 async function loadCodemap ( filePath ) {
@@ -480,14 +485,6 @@ <h2>輸入法模式:</h2>
480485 candidateArea . innerHTML = '' ;
481486 currentCandidates = currentCodemap [ currentCode ] || [ ] ;
482487
483- if ( currentCode . length > 0 ) {
484- const codeSpan = document . createElement ( 'span' ) ;
485- codeSpan . textContent = `[${ currentCode } ]` ;
486- codeSpan . style . color = '#7986cb' ;
487- codeSpan . style . fontWeight = 'bold' ;
488- candidateArea . appendChild ( codeSpan ) ;
489- }
490-
491488 if ( currentCandidates . length > 0 ) {
492489 currentCandidates . forEach ( ( char , index ) => {
493490 const candidateSpan = document . createElement ( 'span' ) ;
@@ -496,11 +493,8 @@ <h2>輸入法模式:</h2>
496493 candidateSpan . dataset . index = index ;
497494 candidateArea . appendChild ( candidateSpan ) ;
498495 } ) ;
499- } else if ( currentCode . length > 0 ) {
500- const noCandidatesSpan = document . createElement ( 'span' ) ;
501- noCandidatesSpan . textContent = '找不到字' ;
502- noCandidatesSpan . style . color = 'red' ;
503- candidateArea . appendChild ( noCandidatesSpan ) ;
496+ } else if ( currentCode . length === 3 ) {
497+ resetInputState ( ) ;
504498 }
505499 }
506500
@@ -570,14 +564,14 @@ <h2>輸入法模式:</h2>
570564 ' ' : ' ' ,
571565 'shift' : 'shift' ,
572566 '~' : '~' ,
573- '!' : '1' , '@' : '2' , '#' : '3' , '$' : '4' , '%' : '5' , '^' : '6' , '&' : '7' , '*' : '8' , '(' : '9' , ')' : '0' ,
574- '-' : '-' , '=' : '=' , '[' : '[' , ']' : ']' , '\\' : '\\' , ' ;' : ';' , "'" : "'" , ',' : ',' , '.' : '.' , '/' : '/' ,
567+ '1' : '1' , '2' : '2' , '3' : '3' , '4' : '4' , '5' : '5' , '6' : '6' , '7' : '7' , '8' : '8' , '9' : '9' , '0' : '0' ,
568+ ';' : ';' , "'" : "'" , ',' : ',' , '.' : '.' , '/' : '/' ,
575569 'q' : 'q' , 'w' : 'w' , 'e' : 'e' , 'r' : 'r' , 't' : 't' , 'y' : 'y' , 'u' : 'u' , 'i' : 'i' , 'o' : 'o' , 'p' : 'p' ,
576- 'a' : 'a' , 's' : 's' , 'd' : 'd' , 'f' : 'f' , 'g' : 'g' , 'h' : 'h' , 'j' : 'j' , 'k' : 'k ' , 'l' : 'l' ,
570+ 'a' : 'a' , 's' : 's' , 'd' : 'd' , 'f' : 'f' , 'g' : 'g' , 'h' : 'h' , 'j' : 'j' , 'k' : 'K ' , 'l' : 'l' ,
577571 'z' : 'z' , 'x' : 'x' , 'c' : 'c' , 'v' : 'v' , 'b' : 'b' , 'n' : 'n' , 'm' : 'm'
578572 } ;
579573 const inputKey = keyMap [ key ] || key ;
580-
574+
581575 // 處理按鍵反白效果
582576 const pressedKey = document . querySelector ( `.key[data-code='${ inputKey } ']` ) ;
583577 if ( pressedKey ) {
@@ -607,19 +601,21 @@ <h2>輸入法模式:</h2>
607601 }
608602
609603 // 在中文模式下處理
610- // 修正:處理數字鍵的邏輯
611- if ( event . key >= '0 ' && event . key <= '9' ) {
604+ // 數字鍵處理邏輯:有候選字則選字,無候選字則上字
605+ if ( event . key >= '1 ' && event . key <= '9' ) {
612606 event . preventDefault ( ) ; // 阻止瀏覽器默認行為
613607 const index = parseInt ( event . key , 10 ) - 1 ;
614608 if ( currentCandidates . length > 0 && index >= 0 && index < currentCandidates . length ) {
615- // 有候選字,且數字鍵在範圍內,則選字
616609 output . textContent += currentCandidates [ index ] ;
617610 resetInputState ( ) ;
618611 } else {
619- // 無候選字,直接輸出數字
620612 output . textContent += event . key ;
621613 resetInputState ( ) ;
622614 }
615+ } else if ( event . key === '0' ) {
616+ event . preventDefault ( ) ;
617+ output . textContent += event . key ;
618+ resetInputState ( ) ;
623619 } else if ( inputKey ) {
624620 event . preventDefault ( ) ; // 阻止瀏覽器默認行為
625621 handleKey ( inputKey ) ;
@@ -632,7 +628,16 @@ <h2>輸入法模式:</h2>
632628 document . addEventListener ( 'keyup' , ( event ) => {
633629 const key = event . key . toLowerCase ( ) ;
634630 const keyMap = {
635- // ... key mappings
631+ 'backspace' : 'backspace' ,
632+ 'enter' : 'enter' ,
633+ ' ' : ' ' ,
634+ 'shift' : 'shift' ,
635+ '~' : '~' ,
636+ '1' : '1' , '2' : '2' , '3' : '3' , '4' : '4' , '5' : '5' , '6' : '6' , '7' : '7' , '8' : '8' , '9' : '9' , '0' : '0' ,
637+ ';' : ';' , "'" : "'" , ',' : ',' , '.' : '.' , '/' : '/' ,
638+ 'q' : 'q' , 'w' : 'w' , 'e' : 'e' , 'r' : 'r' , 't' : 't' , 'y' : 'y' , 'u' : 'u' , 'i' : 'i' , 'o' : 'o' , 'p' : 'p' ,
639+ 'a' : 'a' , 's' : 's' , 'd' : 'd' , 'f' : 'f' , 'g' : 'g' , 'h' : 'h' , 'j' : 'j' , 'k' : 'K' , 'l' : 'l' ,
640+ 'z' : 'z' , 'x' : 'x' , 'c' : 'c' , 'v' : 'v' , 'b' : 'b' , 'n' : 'n' , 'm' : 'm'
636641 } ;
637642 const releasedKey = keyMap [ key ] || key ;
638643 const releasedKeyDiv = document . querySelector ( `.key[data-code='${ releasedKey } ']` ) ;
@@ -643,16 +648,18 @@ <h2>輸入法模式:</h2>
643648
644649 // 處理鍵盤與物理鍵盤的共用邏輯
645650 function handleKey ( key ) {
646- const validCodeKeys = "abcdefghijklmnopqrstuvwxyz ;',./" ;
651+ const validCodeKeys = "1234567890abcdefghijklmnopqrstuvwxyz ;',./" ;
647652
648653 if ( validCodeKeys . includes ( key ) && currentCode . length < 3 ) {
649654 currentCode += key ;
650- mainInput . value = currentCode ;
655+ const fullWidthCode = Array . from ( currentCode ) . map ( char => fullWidthMap [ char ] || char ) . join ( '' ) ;
656+ mainInput . value = fullWidthCode ;
651657 updateCandidates ( ) ;
652658 } else if ( key === 'backspace' ) {
653659 if ( currentCode . length > 0 ) {
654660 currentCode = currentCode . slice ( 0 , - 1 ) ;
655- mainInput . value = currentCode ;
661+ const fullWidthCode = Array . from ( currentCode ) . map ( char => fullWidthMap [ char ] || char ) . join ( '' ) ;
662+ mainInput . value = fullWidthCode ;
656663 updateCandidates ( ) ;
657664 } else {
658665 output . textContent = output . textContent . slice ( 0 , - 1 ) ;
0 commit comments