Skip to content

fix(useKeyPress): cannot listen to modifier key keyup events with exactMatch - #2944

Merged
crazylxr merged 2 commits into
alibaba:masterfrom
Muhamed-Ragab:fix/issue-2704
Aug 30, 2026
Merged

crazylxr merged 2 commits into
alibaba:masterfrom
Muhamed-Ragab:fix/issue-2704

Conversation

@Muhamed-Ragab

Copy link
Copy Markdown
Contributor

Background

useKeyPress('shift', handler, { events: ['keyup'], exactMatch: true }) does not fire because event.shiftKey is false on keyup (the key is already released).

Root Cause

The modifierKey.shift, .ctrl, and .alt handlers check event.shiftKey, event.ctrlKey, event.altKey which are false on keyup. While modifierKey.meta already had special handling for keyup (checking event.keyCode instead), the other modifiers did not.

Changes

packages/hooks/src/useKeyPress/index.ts

  • Added event.type === 'keyup' check to modifierKey.shift, .ctrl, .alt (following the existing pattern for meta)
  • On keyup: check event.keyCode === 16/17/18 to detect the modifier key
  • On keydown: use event.shiftKey/event.ctrlKey/event.altKey (unchanged)

packages/hooks/src/useKeyPress/tests/index.spec.tsx

  • Added 4 tests: shift/ctrl/alt keyup with exactMatch + combination keys regression guard

Verification

  • All 14 tests pass (10 existing + 4 new)

Related

Closes #2704

Submitted by Cursor

- Add event.type === 'keyup' check in modifierKey.shift, .ctrl, .alt
  following the existing pattern for meta
- On keyup: use event.keyCode to detect the modifier key
- On keydown: use event.shiftKey, event.ctrlKey, event.altKey (unchanged)

close alibaba#2704
@CLAassistant

CLAassistant commented Jun 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread packages/hooks/src/useKeyPress/index.ts Outdated

@crazylxr crazylxr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pkg-pr-new

pkg-pr-new Bot commented Aug 30, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2944
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2944

commit: 0288e8d

@crazylxr
crazylxr merged commit 0a5939b into alibaba:master Aug 30, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[useKeyPress] can't listen shift keyup

3 participants