@@ -24,6 +24,8 @@ function getHangmanHTML() {
2424 </div>
2525
2626 <div class="keyboard" id="keyboard"></div>
27+
28+ <p class="keyboard-hint">⌨️ Type any letter key to guess directly</p>
2729
2830 <div class="game-message" id="gameMessage"></div>
2931
@@ -156,6 +158,11 @@ function getHangmanHTML() {
156158 .key-btn.wrong {
157159 background: var(--danger-color);
158160 }
161+
162+ .key-btn.key-flash {
163+ transform: scale(1.2);
164+ box-shadow: 0 3px 10px rgba(99, 102, 241, 0.6);
165+ }
159166
160167 .game-message {
161168 font-size: 1.5rem;
@@ -171,6 +178,24 @@ function getHangmanHTML() {
171178 .game-message.lose {
172179 color: var(--danger-color);
173180 }
181+
182+ .keyboard-hint {
183+ font-size: 0.9rem;
184+ color: var(--text-secondary);
185+ margin-bottom: 1rem;
186+ }
187+
188+ .keyboard-hint kbd {
189+ display: inline-block;
190+ padding: 0.15rem 0.45rem;
191+ font-size: 0.85rem;
192+ font-family: monospace;
193+ background: var(--surface-color);
194+ border: 1px solid var(--border-color);
195+ border-radius: 5px;
196+ color: var(--primary-color);
197+ font-weight: bold;
198+ }
174199
175200 .btn-new-game {
176201 background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
@@ -340,11 +365,16 @@ function initHangman() {
340365 guessedLetters . push ( letter ) ;
341366
342367 const btn = keyboard . querySelector ( `[data-letter="${ letter } "]` ) ;
343- btn . disabled = true ;
368+ if ( btn ) {
369+ btn . disabled = true ;
370+ // Flash the button briefly when triggered by keyboard
371+ btn . classList . add ( 'key-flash' ) ;
372+ setTimeout ( ( ) => btn . classList . remove ( 'key-flash' ) , 150 ) ;
373+ }
344374
345375 if ( currentWord . includes ( letter ) ) {
346376 correctLetters . push ( letter ) ;
347- btn . classList . add ( 'correct' ) ;
377+ btn && btn . classList . add ( 'correct' ) ;
348378
349379 updateWordDisplay ( ) ;
350380
@@ -356,7 +386,7 @@ function initHangman() {
356386 }
357387 } else {
358388 wrongAttempts ++ ;
359- btn . classList . add ( 'wrong' ) ;
389+ btn && btn . classList . add ( 'wrong' ) ;
360390
361391 const drawStage = wrongAttempts + 4 ;
362392 drawHangman ( drawStage ) ;
@@ -368,7 +398,6 @@ function initHangman() {
368398 gameMessage . innerHTML = `😔 Game Over! The word was: <strong>${ currentWord . toUpperCase ( ) } </strong>` ;
369399 gameMessage . className = 'game-message lose' ;
370400 disableAllKeys ( ) ;
371- updateWordDisplay ( ) ;
372401 wordDisplay . innerHTML = '' ;
373402 for ( let letter of currentWord ) {
374403 const letterBox = document . createElement ( 'div' ) ;
@@ -400,15 +429,28 @@ function initHangman() {
400429 initGame ( ) ;
401430 drawGallows ( ) ;
402431 } ) ;
403-
404- document . addEventListener ( 'keypress' , ( e ) => {
432+
433+ // Keyboard handler — using keydown instead of deprecated keypress
434+ function handleKeydown ( e ) {
405435 if ( gameOver ) return ;
436+ // Ignore if user is typing in an input field
437+ if ( e . target . tagName === 'INPUT' || e . target . tagName === 'TEXTAREA' ) return ;
406438 const letter = e . key . toLowerCase ( ) ;
407439 if ( / ^ [ a - z ] $ / . test ( letter ) && ! guessedLetters . includes ( letter ) ) {
408440 guessLetter ( letter ) ;
409441 }
442+ }
443+ document . addEventListener ( 'keydown' , handleKeydown ) ;
444+
445+ // Clean up listener when modal closes
446+ const observer = new MutationObserver ( ( ) => {
447+ if ( ! document . getElementById ( 'newGameBtn' ) ) {
448+ document . removeEventListener ( 'keydown' , handleKeydown ) ;
449+ observer . disconnect ( ) ;
450+ }
410451 } ) ;
452+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
411453
412454 initGame ( ) ;
413455 drawGallows ( ) ;
414- }
456+ }
0 commit comments