|
| 1 | +# useKey |
| 2 | + |
| 3 | +Vue function that executes a handler when a keyboard key is pressed. |
| 4 | + |
| 5 | +## Reference |
| 6 | + |
| 7 | +```typescript |
| 8 | +type UseKeyFilter = string | ((event: KeyboardEvent) => boolean) |
| 9 | +``` |
| 10 | +
|
| 11 | +```typescript |
| 12 | +function useKey( |
| 13 | + filter: UseKeyFilter, |
| 14 | + callback?: any, |
| 15 | + runOnMount?: boolean |
| 16 | +): { |
| 17 | + isPressed: Ref<boolean> |
| 18 | + isTracking: Ref<boolean> |
| 19 | + start: () => void |
| 20 | + stop: () => void |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +### Parameters |
| 25 | + |
| 26 | +- `filter: UseKeyFilter` the filter string or function to use for triggering the key event |
| 27 | +- `callback: Function` the function called when the given key is pressed |
| 28 | +- `runOnMount: boolean` whether to track the given key on mount, `true` by default. |
| 29 | + |
| 30 | +### Returns |
| 31 | + |
| 32 | +- `isPressed: Ref<boolean>` whether the key is currently pressed or not |
| 33 | +- `isTracking: Ref<boolean>` whether this function events are running or not |
| 34 | +- `start: Function` the function used for start tracking the key event |
| 35 | +- `stop: Function` the function used for stop tracking the key event |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +Example where se use the `callback` and when pressing the key without |
| 40 | +releasing **it will update the value continuously**. |
| 41 | + |
| 42 | +```html |
| 43 | +<template> |
| 44 | + <div> |
| 45 | + <p>isPressed {{ isPressed }}</p> |
| 46 | + <p>keyPressCount {{ keyPressCount }}</p> |
| 47 | + <div> |
| 48 | + <button @click="start" v-if="!isTracking"> |
| 49 | + Start tracking key press |
| 50 | + </button> |
| 51 | + <button @click="stop" v-else> |
| 52 | + Stop tracking key press |
| 53 | + </button> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | +</template> |
| 57 | + |
| 58 | +<script lang="ts"> |
| 59 | + import Vue from 'vue' |
| 60 | + import { ref } from '@src/api' |
| 61 | + import { useKey } from 'vue-use-kit' |
| 62 | +
|
| 63 | + export default Vue.extend({ |
| 64 | + name: 'UseKeyDemo', |
| 65 | + setup() { |
| 66 | + const keyPressCount = ref(0) |
| 67 | +
|
| 68 | + // Increase value continuously when 'a' key is kept pressed |
| 69 | + const handleKey = () => keyPressCount.value++ |
| 70 | +
|
| 71 | + const { isPressed, isTracking, start, stop } = useKey('a', handleKey) |
| 72 | + return { keyPressCount, isPressed, isTracking, start, stop } |
| 73 | + } |
| 74 | + }) |
| 75 | +</script> |
| 76 | +``` |
| 77 | + |
| 78 | +Example where se use the `callback` and when pressing the key |
| 79 | +**it will update the value only on keyUp**. |
| 80 | + |
| 81 | +```html |
| 82 | +<template> |
| 83 | + <div> |
| 84 | + <p>isPressed {{ isPressed }}</p> |
| 85 | + <p>keyPressCount {{ keyPressCount }}</p> |
| 86 | + <div> |
| 87 | + <button @click="start" v-if="!isTracking"> |
| 88 | + Start tracking key press |
| 89 | + </button> |
| 90 | + <button @click="stop" v-else> |
| 91 | + Stop tracking key press |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | +</template> |
| 96 | + |
| 97 | +<script lang="ts"> |
| 98 | + import Vue from 'vue' |
| 99 | + import { ref } from '@src/api' |
| 100 | + import { useKey } from 'vue-use-kit' |
| 101 | +
|
| 102 | + export default Vue.extend({ |
| 103 | + name: 'UseKeyDemo', |
| 104 | + setup() { |
| 105 | + const keyPressCount = ref(0) |
| 106 | +
|
| 107 | + // Increase value when 'a' key is released |
| 108 | + const handleKey = e => { |
| 109 | + if (e.type === 'keyup') keyPressCount.value++ |
| 110 | + } |
| 111 | +
|
| 112 | + const { isPressed, isTracking, start, stop } = useKey('a', handleKey) |
| 113 | + return { keyPressCount, isPressed, isTracking, start, stop } |
| 114 | + } |
| 115 | + }) |
| 116 | +</script> |
| 117 | +``` |
| 118 | + |
| 119 | +Example where we `watch` the `isPressed` value and when pressing |
| 120 | +the key without releasing **it will update the value only once**. |
| 121 | + |
| 122 | +```html |
| 123 | +<template> |
| 124 | + <div> |
| 125 | + <p>isPressed {{ isPressed }}</p> |
| 126 | + <p>keyPressCount {{ keyPressCount }}</p> |
| 127 | + <div> |
| 128 | + <button @click="start" v-if="!isTracking"> |
| 129 | + Start tracking key press |
| 130 | + </button> |
| 131 | + <button @click="stop" v-else> |
| 132 | + Stop tracking key press |
| 133 | + </button> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | +</template> |
| 137 | + |
| 138 | +<script lang="ts"> |
| 139 | + import Vue from 'vue' |
| 140 | + import { watch, ref } from '@src/api' |
| 141 | + import { useKey } from 'vue-use-kit' |
| 142 | +
|
| 143 | + export default Vue.extend({ |
| 144 | + name: 'UseKeyDemo', |
| 145 | + setup() { |
| 146 | + const keyPressCount = ref(0) |
| 147 | + const { isPressed, isTracking, start, stop } = useKey('a') |
| 148 | +
|
| 149 | + // Increase value when 'a' key is pressed |
| 150 | + watch(isPressed, isPress => isPress && keyPressCount.value++) |
| 151 | +
|
| 152 | + return { keyPressCount, isPressed, isTracking, start, stop } |
| 153 | + } |
| 154 | + }) |
| 155 | +</script> |
| 156 | +``` |
0 commit comments