diff --git a/cocos/core/platform/event-manager/events.ts b/cocos/core/platform/event-manager/events.ts index 2e935194d7..b40f3a7de2 100644 --- a/cocos/core/platform/event-manager/events.ts +++ b/cocos/core/platform/event-manager/events.ts @@ -35,6 +35,7 @@ import { Touch } from './touch'; import { Acceleration } from './acceleration'; import { legacyCC } from '../../global-exports'; import { SystemEventTypeUnion, SystemEventType } from './event-enum'; +import { KeyCode } from './key-code'; const _vec2 = new Vec2(); @@ -597,15 +598,10 @@ export class EventAcceleration extends Event { */ export class EventKeyboard extends Event { /** - * @en The keyCode read-only property represents a system and implementation dependent numerical code - * identifying the unmodified value of the pressed key. - * This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key. - * If the key can't be identified, this value is 0. - * @zh keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。 - * 这通常是十进制 ASCII (RFC20) 或者 Windows 1252 代码,所对应的密钥。 - * 如果无法识别该键,则该值为 0。 + * @en The KeyCode enum value of current keyboard event. + * @zh 当前键盘事件的 KeyCode 枚举值 */ - public keyCode: number; + public keyCode: KeyCode; /** * @en Raw DOM KeyboardEvent. @@ -637,7 +633,7 @@ export class EventKeyboard extends Event { * @param eventType - The type of the event * @param bubbles - Indicates whether the event bubbles up through the hierarchy or not. */ - constructor (keyCode: number | KeyboardEvent, eventType: SystemEventTypeUnion, bubbles?: boolean); + constructor (keyCode: KeyCode | KeyboardEvent, eventType: SystemEventTypeUnion, bubbles?: boolean); constructor (keyCode: any, eventType: SystemEventTypeUnion | boolean, bubbles?: boolean) { if (typeof eventType === 'boolean') { const isPressed = eventType; diff --git a/cocos/core/platform/event-manager/system-event.ts b/cocos/core/platform/event-manager/system-event.ts index 6375087722..5c256dc5b9 100644 --- a/cocos/core/platform/event-manager/system-event.ts +++ b/cocos/core/platform/event-manager/system-event.ts @@ -53,6 +53,20 @@ export declare namespace SystemEvent { export type EventType = EnumAlias; } +interface SystemEventMap { + [SystemEvent.EventType.MOUSE_DOWN]: (event: EventMouse) => void, + [SystemEvent.EventType.MOUSE_MOVE]: (event: EventMouse) => void, + [SystemEvent.EventType.MOUSE_UP]: (event: EventMouse) => void, + [SystemEvent.EventType.MOUSE_WHEEL]: (event: EventMouse) => void, + [SystemEvent.EventType.TOUCH_START]: (touch: Touch, event: EventTouch) => void, + [SystemEvent.EventType.TOUCH_MOVE]: (touch: Touch, event: EventTouch) => void, + [SystemEvent.EventType.TOUCH_END]: (touch: Touch, event: EventTouch) => void, + [SystemEvent.EventType.TOUCH_CANCEL]: (touch: Touch, event: EventTouch) => void, + [SystemEvent.EventType.KEY_DOWN]: (event: EventKeyboard) => void, + [SystemEvent.EventType.KEY_UP]: (event: EventKeyboard) => void, + [SystemEvent.EventType.DEVICEMOTION]: (event: EventAcceleration) => void, +} + /** * @en * The System event, it currently supports keyboard events and accelerometer events.
@@ -124,7 +138,8 @@ export class SystemEvent extends EventTarget { * @param callback - The event listener's callback * @param target - The event listener's target and callee */ - public on void> (type: SystemEvent.EventType, callback: TFunction, target?, once?: boolean) { + // @ts-expect-error Property 'on' in type 'SystemEvent' is not assignable to the same property in base type + public on (type: K, callback: SystemEventMap[K], target?: any, once?: boolean) { if (EDITOR && !legacyCC.GAME_VIEW) { return callback; } @@ -229,7 +244,7 @@ export class SystemEvent extends EventTarget { * @param callback - The callback to remove. * @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed */ - public off void> (type: SystemEvent.EventType, callback?: TFunction, target?) { + public off (type: K, callback?: SystemEventMap[K], target?: any) { if (EDITOR && !legacyCC.GAME_VIEW) { return; }