mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-30 03:51:25 -08:00
Refactor Artplayer class to add keydown event listener
This commit is contained in:
1 parent
0ddb7751a1
commit
b6df6a6148
9 files changed
+87
-42
No files matched your search
@@ -13,33 +13,33 @@ export default class Hotkey {
|
||||
init() {
|
||||
const { proxy, constructor } = this.art;
|
||||
|
||||
this.add(27, () => {
|
||||
this.add('Escape', () => {
|
||||
if (this.art.fullscreenWeb) {
|
||||
this.art.fullscreenWeb = false;
|
||||
}
|
||||
});
|
||||
|
||||
this.add(32, () => {
|
||||
this.add('Space', () => {
|
||||
this.art.toggle();
|
||||
});
|
||||
|
||||
this.add(37, () => {
|
||||
this.add('ArrowLeft', () => {
|
||||
this.art.backward = constructor.SEEK_STEP;
|
||||
});
|
||||
|
||||
this.add(38, () => {
|
||||
this.add('ArrowUp', () => {
|
||||
this.art.volume += constructor.VOLUME_STEP;
|
||||
});
|
||||
|
||||
this.add(39, () => {
|
||||
this.add('ArrowRight', () => {
|
||||
this.art.forward = constructor.SEEK_STEP;
|
||||
});
|
||||
|
||||
this.add(40, () => {
|
||||
this.add('ArrowDown', () => {
|
||||
this.art.volume -= constructor.VOLUME_STEP;
|
||||
});
|
||||
|
||||
proxy(window, 'keydown', (event) => {
|
||||
proxy(document, 'keydown', (event) => {
|
||||
if (this.art.isFocus) {
|
||||
const tag = document.activeElement.tagName.toUpperCase();
|
||||
const editable = document.activeElement.getAttribute('contenteditable');
|
||||
@@ -53,7 +53,7 @@ export default class Hotkey {
|
||||
!event.metaKey &&
|
||||
!event.shiftKey
|
||||
) {
|
||||
const events = this.keys[event.keyCode];
|
||||
const events = this.keys[event.code];
|
||||
if (events) {
|
||||
event.preventDefault();
|
||||
for (let index = 0; index < events.length; index++) {
|
||||
@@ -63,6 +63,7 @@ export default class Hotkey {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.art.emit('keydown', event);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -165,8 +165,8 @@ declare class Artplayer extends Player {
|
||||
|
||||
readonly hotkey: {
|
||||
keys: Record<string, ((event: Event) => any)[]>;
|
||||
add(key: number, callback: (this: Artplayer, event: Event) => any): Artplayer['hotkey'];
|
||||
remove(key: number, callback: Function): Artplayer['hotkey'];
|
||||
add(key: string, callback: (this: Artplayer, event: Event) => any): Artplayer['hotkey'];
|
||||
remove(key: string, callback: Function): Artplayer['hotkey'];
|
||||
};
|
||||
|
||||
readonly mask: Component;
|
||||
|
||||
Vendored
+1
@@ -68,4 +68,5 @@ export type Events = {
|
||||
restart: [url: string];
|
||||
muted: [state: boolean];
|
||||
setBar: [type: Bar, percentage: number, event?: Event];
|
||||
keydown: [event: KeyboardEvent];
|
||||
};
|
||||
Reference in new issue
Block a user