-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yang
committed
Apr 4, 2021
1 parent
1fbb7e1
commit b3cd933
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue-demi' | ||
import { useKey } from '.' | ||
const translateX = ref(0) | ||
const translateY = ref(0) | ||
useKey('ArrowUp', (e: KeyboardEvent) => { | ||
translateY.value -= 10 | ||
e.preventDefault() | ||
}) | ||
useKey('ArrowDown', (e: KeyboardEvent) => { | ||
translateY.value += 10 | ||
e.preventDefault() | ||
}) | ||
useKey('ArrowLeft', (e: KeyboardEvent) => { | ||
translateX.value -= 10 | ||
e.preventDefault() | ||
}) | ||
useKey('ArrowRight', (e: KeyboardEvent) => { | ||
translateX.value += 10 | ||
e.preventDefault() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="container"> | ||
<div class="ball" :style="{transform: `translate(${translateX}px, ${translateY}px)`}" /> | ||
</div> | ||
<div class="text-center mt-4"> | ||
Use the arrow keys to control the movement of the ball. | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.container { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
width: 400px; | ||
height: 100px; | ||
margin: auto; | ||
overflow: hidden; | ||
border: 1px solid #333; | ||
} | ||
.ball { | ||
width: 16px; | ||
height: 16px; | ||
background: #333; | ||
border-radius: 50%; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# useKey | ||
Listen for keyboard keys being used. | ||
|
||
## Usage | ||
|
||
```js | ||
import { useKey } from '@vueuse/core' | ||
|
||
useKey('ArrowDown', (e) => { | ||
e.preventDefault() | ||
}) | ||
``` | ||
|
||
Custom Event Target | ||
|
||
```js | ||
useKey(document, 'ArrowDown', (e) => { | ||
e.preventDefault() | ||
}) | ||
``` | ||
|
||
Custom Keyboard Event | ||
```js | ||
useKey('ArrowDown', (e) => { | ||
e.preventDefault() | ||
}, 'keyUp') | ||
``` | ||
|
||
## Type Declarations | ||
|
||
```typescript | ||
export type KeyPredicate = (event: KeyboardEvent) => boolean | ||
export type KeyFilter = null | undefined | string | ((event: KeyboardEvent) => boolean) | ||
export type Handler = (event: KeyboardEvent) => void | ||
export type EventType = 'keydown' | 'keypress' | 'keyup' | ||
|
||
export function useKey( | ||
target: MaybeRef<EventTarget | null | undefined>, | ||
key: KeyFilter, | ||
handler: Handler, | ||
event?: EventType, | ||
options?: boolean | AddEventListenerOptions | ||
): Fn | ||
|
||
export function useKey( | ||
key: KeyFilter, | ||
handler: Handler, | ||
event?: EventType, | ||
options?: boolean | AddEventListenerOptions | ||
): Fn | ||
``` | ||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/core/useKey/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/core/useKey/index.md) | ||
|
||
<!--FOOTER_ENDS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
useEventListener, | ||
} from '../useEventListener' | ||
import { defaultWindow } from '../_configurable' | ||
import { Fn, isFunction, isString, MaybeRef } from '@vueuse/shared' | ||
|
||
export type KeyPredicate = (event: KeyboardEvent) => boolean | ||
export type KeyFilter = null | undefined | string | ((event: KeyboardEvent) => boolean) | ||
export type Handler = (event: KeyboardEvent) => void | ||
export type EventType = 'keydown' | 'keypress' | 'keyup' | ||
|
||
const createKeyPredicate = (keyFilter: KeyFilter): KeyPredicate => | ||
typeof keyFilter === 'function' | ||
? keyFilter | ||
: typeof keyFilter === 'string' | ||
? (event: KeyboardEvent) => event.key === keyFilter | ||
: keyFilter | ||
? () => true | ||
: () => false | ||
|
||
export function useKey( | ||
target: MaybeRef<EventTarget | null | undefined>, | ||
key: KeyFilter, | ||
handler: Handler, | ||
event?: EventType, | ||
options?: boolean | AddEventListenerOptions | ||
): Fn | ||
|
||
export function useKey( | ||
key: KeyFilter, | ||
handler: Handler, | ||
event?: EventType, | ||
options?: boolean | AddEventListenerOptions | ||
): Fn | ||
|
||
export function useKey(...args: any[]) { | ||
let target: MaybeRef<EventTarget> | undefined | ||
let key: KeyFilter | ||
let handler: any | ||
let eventName: EventType | ||
let options: any | ||
|
||
if (isString(args[0]) || isFunction(args[0])) { | ||
[key, handler, eventName = 'keydown', options] = args | ||
target = defaultWindow | ||
} | ||
else { | ||
[target, key, handler, eventName = 'keydown', options] = args | ||
target = defaultWindow | ||
} | ||
|
||
const predicate = createKeyPredicate(key) | ||
const listener = (e: KeyboardEvent) => { | ||
if (predicate(e)) | ||
handler(e) | ||
} | ||
|
||
return useEventListener(target, eventName, listener, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters