@@ -928,6 +928,87 @@ export class FocusEvent extends Event {
928928 }
929929}
930930
931+ export interface KeyboardEventInit extends EventOptions {
932+ key ?: string ;
933+ code ?: string ;
934+ keyCode ?: number ;
935+ location ?: number ;
936+ repeat ?: boolean ;
937+ isComposing ?: boolean ;
938+ altKey ?: boolean ;
939+ ctrlKey ?: boolean ;
940+ metaKey ?: boolean ;
941+ shiftKey ?: boolean ;
942+ }
943+
944+ export class KeyboardEvent extends Event {
945+ static readonly DOM_KEY_LOCATION_STANDARD = 0 ;
946+ static readonly DOM_KEY_LOCATION_LEFT = 1 ;
947+ static readonly DOM_KEY_LOCATION_RIGHT = 2 ;
948+ static readonly DOM_KEY_LOCATION_NUMPAD = 3 ;
949+
950+ readonly DOM_KEY_LOCATION_STANDARD = KeyboardEvent . DOM_KEY_LOCATION_STANDARD ;
951+ readonly DOM_KEY_LOCATION_LEFT = KeyboardEvent . DOM_KEY_LOCATION_LEFT ;
952+ readonly DOM_KEY_LOCATION_RIGHT = KeyboardEvent . DOM_KEY_LOCATION_RIGHT ;
953+ readonly DOM_KEY_LOCATION_NUMPAD = KeyboardEvent . DOM_KEY_LOCATION_NUMPAD ;
954+
955+ readonly #keyCode: number ;
956+ readonly key : string ;
957+ readonly code : string ;
958+ readonly location : number ;
959+ readonly repeat : boolean ;
960+ readonly isComposing : boolean ;
961+ readonly altKey : boolean ;
962+ readonly ctrlKey : boolean ;
963+ readonly metaKey : boolean ;
964+ readonly shiftKey : boolean ;
965+
966+ constructor ( type : string , options : KeyboardEventInit = { } ) {
967+ super ( type , options ) ;
968+ this . key = options . key ?? '' ;
969+ this . code = options . code ?? '' ;
970+ this . #keyCode = ( options . keyCode ?? 0 ) >>> 0 ;
971+ this . location = options . location ?? 0 ;
972+ this . repeat = options . repeat ?? false ;
973+ this . isComposing = options . isComposing ?? false ;
974+ this . altKey = options . altKey ?? false ;
975+ this . ctrlKey = options . ctrlKey ?? false ;
976+ this . metaKey = options . metaKey ?? false ;
977+ this . shiftKey = options . shiftKey ?? false ;
978+ }
979+
980+ get keyCode ( ) : number {
981+ return this . #keyCode;
982+ }
983+
984+ getModifierState ( key : string ) : boolean {
985+ switch ( key ) {
986+ case 'Alt' :
987+ return this . altKey ;
988+ case 'Control' :
989+ return this . ctrlKey ;
990+ case 'Meta' :
991+ return this . metaKey ;
992+ case 'Shift' :
993+ return this . shiftKey ;
994+ default :
995+ return false ;
996+ }
997+ }
998+ }
999+
1000+ export function nativeKeyCode ( event : { key : string ; code ?: string } ) : number {
1001+ if ( / ^ [ a - z ] $ / i. test ( event . key ) ) {
1002+ return event . key . toUpperCase ( ) . charCodeAt ( 0 ) ;
1003+ }
1004+ if ( / ^ [ 0 - 9 ] $ / . test ( event . key ) ) {
1005+ return event . key . charCodeAt ( 0 ) ;
1006+ }
1007+ if ( event . code && / ^ D i g i t [ 0 - 9 ] $ / . test ( event . code ) ) {
1008+ return event . code . charCodeAt ( 5 ) ;
1009+ }
1010+ return fixedKeyCodes . get ( event . key ) ?? 0 ;
1011+ }
9311012/* HTML */
9321013
9331014export function createHTMLDocument ( notify : Node [ 'notify' ] , title ?: string ) : HTMLDocument {
@@ -1138,7 +1219,9 @@ export class HTMLAnchorElement extends mixin(HTMLElement, [Focusable]) {}
11381219
11391220export class HTMLButtonElement extends mixin ( HTMLElement , [ Disablable , Focusable ] ) { }
11401221
1141- export class HTMLInputElement extends mixin ( HTMLElement , [ Disablable , Focusable ] ) { }
1222+ export class HTMLInputElement extends mixin ( HTMLElement , [ Disablable , Focusable ] ) {
1223+ value = '' ;
1224+ }
11421225
11431226export class HTMLSelectElement extends mixin ( HTMLElement , [ Disablable , Focusable ] ) { }
11441227
@@ -1260,7 +1343,7 @@ function transitionActiveElement(document: Document, to: Element | null): void {
12601343 }
12611344}
12621345
1263- function isFocusable ( element : HTMLElement ) : boolean {
1346+ export function isFocusable ( element : HTMLElement ) : boolean {
12641347 if ( ! element . isConnected ) {
12651348 return false ;
12661349 }
@@ -1614,3 +1697,24 @@ interface DispatchState {
16141697const eventStates = new WeakMap < Event , DispatchState > ( ) ;
16151698const eventRegistries = new WeakMap < EventTarget , ListenerRegistry > ( ) ;
16161699const eventHosts = new WeakMap < Document , EventTarget > ( ) ;
1700+
1701+ const fixedKeyCodes = new Map < string , number > ( [
1702+ [ 'Backspace' , 8 ] ,
1703+ [ 'Tab' , 9 ] ,
1704+ [ 'Enter' , 13 ] ,
1705+ [ 'Shift' , 16 ] ,
1706+ [ 'Control' , 17 ] ,
1707+ [ 'Alt' , 18 ] ,
1708+ [ 'CapsLock' , 20 ] ,
1709+ [ 'Escape' , 27 ] ,
1710+ [ ' ' , 32 ] ,
1711+ [ 'PageUp' , 33 ] ,
1712+ [ 'PageDown' , 34 ] ,
1713+ [ 'End' , 35 ] ,
1714+ [ 'Home' , 36 ] ,
1715+ [ 'ArrowLeft' , 37 ] ,
1716+ [ 'ArrowUp' , 38 ] ,
1717+ [ 'ArrowRight' , 39 ] ,
1718+ [ 'ArrowDown' , 40 ] ,
1719+ [ 'Delete' , 46 ] ,
1720+ ] ) ;
0 commit comments