Skip to content

useKeyModifier

类别
导出大小
671 B
最近修改
2 days ago

响应式 修饰符状态。跟踪任何 支持的修饰符 的状态——请参阅浏览器兼容性说明。

🌐 Reactive Modifier State. Tracks state of any of the supported modifiers - see Browser Compatibility notes.

通过 Vue School 的免费视频课程学习 useKeyModifier 的使用!

示例

capsLock
numLock
scrollLock
shift
control
alt

用法

🌐 Usage

ts
import { 
useKeyModifier
} from '@vueuse/core'
const
capsLockState
=
useKeyModifier
('CapsLock')
console
.
log
(
capsLockState
.
value
)

事件

🌐 Events

你可以自定义哪些事件会触发状态更新。默认情况下,这些事件是 mouseupmousedownkeyupkeydown。要自定义这些事件,请执行以下操作:

🌐 You can customize which events will prompt the state to update. By default, these are mouseup, mousedown, keyup, keydown. To customize these events:

ts
import { 
useKeyModifier
} from '@vueuse/core'
const
capsLockState
=
useKeyModifier
('CapsLock', {
events
: ['mouseup', 'mousedown'] })
console
.
log
(
capsLockState
) // null
// Caps Lock turned on with key press
console
.
log
(
capsLockState
) // null
// Mouse button clicked
console
.
log
(
capsLockState
) // true

初始状态

🌐 Initial State

默认情况下,返回的 ref 将是 Ref<null>,直到接收到第一个事件。你可以通过以下方式显式传递初始状态给它:

🌐 By default, the returned ref will be Ref<null> until the first event is received. You can explicitly pass the initial state to it via:

ts
const 
capsLockState1
=
useKeyModifier
('CapsLock') // Ref<boolean | null>
const
capsLockState2
=
useKeyModifier
('CapsLock', {
initial
: false }) // Ref<boolean>