:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(shortcuts): report keybind triggered from legacy hotkey hook (#68281)
Paul D'Ambra41322e406d9f
diff --git a/frontend/src/lib/components/Shortcuts/shortcutLogic.tsx b/frontend/src/lib/components/Shortcuts/shortcutLogic.tsxindex 573a7f9e55da..d74fc657f9ce 100644--- a/frontend/src/lib/components/Shortcuts/shortcutLogic.tsx+++ b/frontend/src/lib/components/Shortcuts/shortcutLogic.tsx@@ -70,6 +70,7 @@ function triggerShortcut(shortcut: ShortcutType, triggeredKeybind: string[]): vokeybind: formatKeybind(triggeredKeybind),interaction: shortcut.interaction,scope: shortcut.scope ?? 'global',+ mechanism: 'shortcut',})if (shortcut.interaction === 'click') {@@ -153,6 +154,8 @@ export const shortcutLogic = kea<shortcutLogicType>([cache.sequenceKeys = []cache.sequenceShortcut = null+ // modifier vocabulary and order must stay in lockstep with formatTriggeredKeybind in+ // lib/hooks/useKeyboardHotkeys.tsx, so the same chord reports the same `keybind` stringconst pressedKeys: string[] = [COMMAND_OR_CTRL]if (event.shiftKey) {pressedKeys.push('shift')diff --git a/frontend/src/lib/hooks/useKeyboardHotkeys.test.tsx b/frontend/src/lib/hooks/useKeyboardHotkeys.test.tsxnew file mode 100644index 000000000000..6a9221a276fe--- /dev/null+++ b/frontend/src/lib/hooks/useKeyboardHotkeys.test.tsx@@ -0,0 +1,152 @@+import { cleanup, renderHook } from '@testing-library/react'++import posthog from 'lib/posthog-typed'++import { useKeyboardHotkeys } from './useKeyboardHotkeys'++jest.mock('lib/posthog-typed', () => ({+ __esModule: true,+ default: { __loaded: true, capture: jest.fn() },+}))++const mockPosthog = posthog as unknown as { __loaded: boolean; capture: jest.Mock }++function press(key: string, init: KeyboardEventInit = {}, target: Element = document.body): void {+ target.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true, ...init }))+}++describe('useKeyboardHotkeys', () => {+ beforeEach(() => {+ mockPosthog.__loaded = true+ mockPosthog.capture.mockClear()+ })++ afterEach(() => {+ // RTL auto-cleanup doesn't run in this jest setup - unmount so window keydown listeners don't leak between tests+ cleanup()+ })++ it('runs the action and captures keybind triggered on a match', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ k: { action } }))++ press('k')++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).toHaveBeenCalledWith('keybind triggered', {+ keybind: 'k',+ mechanism: 'hotkey',+ })+ })++ it('captures the normalized key name for space', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ space: { action } }))++ press(' ')++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).toHaveBeenCalledWith('keybind triggered', {+ keybind: 'space',+ mechanism: 'hotkey',+ })+ })++ it('captures shift chords for hotkeys that do not handle modifiers themselves', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ k: { action } }))++ press('K', { shiftKey: true })++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).toHaveBeenCalledWith('keybind triggered', {+ keybind: 'shift+k',+ mechanism: 'hotkey',+ })+ })++ it('captures modifiers in the same order as shortcutLogic (shift before option)', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ arrowleft: { action, willHandleEvent: true } }))++ press('ArrowLeft', { shiftKey: true, altKey: true })++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).toHaveBeenCalledWith('keybind triggered', {+ keybind: 'shift+option+arrowleft',+ mechanism: 'hotkey',+ })+ })++ it('does not capture held-key repeats but still runs the action', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ arrowleft: { action, willHandleEvent: true } }))++ press('ArrowLeft')+ press('ArrowLeft', { repeat: true })+ press('ArrowLeft', { repeat: true })++ expect(action).toHaveBeenCalledTimes(3)+ expect(mockPosthog.capture).toHaveBeenCalledTimes(1)+ })++ it.each([{ metaKey: true }, { ctrlKey: true }])(+ 'does not capture ctrl/meta chords on willHandleEvent hotkeys, whose actions treat them as browser shortcuts and no-op (%o)',+ (modifiers) => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ arrowleft: { action, willHandleEvent: true } }))++ press('ArrowLeft', modifiers)++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).not.toHaveBeenCalled()+ }+ )++ it('does not run or capture modifier chords for hotkeys that do not handle events', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ k: { action } }))++ press('k', { metaKey: true })+ press('k', { ctrlKey: true })+ press('k', { altKey: true })++ expect(action).not.toHaveBeenCalled()+ expect(mockPosthog.capture).not.toHaveBeenCalled()+ })++ it('does not capture when posthog is uninitialized, as in the toolbar bundle on customer sites', () => {+ mockPosthog.__loaded = false+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ escape: { action, willHandleEvent: true } }))++ press('Escape')++ expect(action).toHaveBeenCalledTimes(1)+ expect(mockPosthog.capture).not.toHaveBeenCalled()+ })++ it('does not run or capture disabled hotkeys', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ k: { action, disabled: true } }))++ press('k')++ expect(action).not.toHaveBeenCalled()+ expect(mockPosthog.capture).not.toHaveBeenCalled()+ })++ it('does not run or capture while typing in an input', () => {+ const action = jest.fn()+ renderHook(() => useKeyboardHotkeys({ k: { action } }))++ const input = document.createElement('input')+ document.body.appendChild(input)+ press('k', {}, input)+ input.remove()++ expect(action).not.toHaveBeenCalled()+ expect(mockPosthog.capture).not.toHaveBeenCalled()+ })+})diff --git a/frontend/src/lib/hooks/useKeyboardHotkeys.tsx b/frontend/src/lib/hooks/useKeyboardHotkeys.tsxindex 83b6c35e6ee7..a4fd74fce578 100644--- a/frontend/src/lib/hooks/useKeyboardHotkeys.tsx+++ b/frontend/src/lib/hooks/useKeyboardHotkeys.tsx@@ -1,6 +1,7 @@import { DependencyList } from 'react'import { useEventListener } from 'lib/hooks/useEventListener'+import posthog from 'lib/posthog-typed'import { HotKey } from '~/types'@@ -32,6 +33,26 @@ const isToolbarInput = (event: Event, ignorableElements: string[]): boolean => {const exceptions = ['.hotkey-block', '.hotkey-block *']+// modifier vocabulary and order must stay in lockstep with shortcutLogic.tsx's pressedKeys,+// so the same chord reports the same `keybind` string from either mechanism+function formatTriggeredKeybind(event: KeyboardEvent, key: string): string {+ const parts: string[] = []+ if (event.metaKey) {+ parts.push('command')+ }+ if (event.ctrlKey) {+ parts.push('ctrl')+ }+ if (event.shiftKey) {+ parts.push('shift')+ }+ if (event.altKey) {+ parts.push('option')+ }+ parts.push(key)+ return parts.join('+')+}+/**** @param hotkeys Hotkeys to listen to and actions to execute@@ -74,6 +95,20 @@ export function useKeyboardHotkeys(hotkeys: HotkeysInterface, deps?: DependencyLif (!hotkey.willHandleEvent) {event.preventDefault()}+ // don't capture: when posthog is uninitialized (the toolbar bundle on customer sites — capturing+ // there only warns in the customer's console), on held-key repeats (seeking by holding an arrow+ // counts once), or on ctrl/meta chords reaching willHandleEvent actions (they treat those as+ // browser shortcuts and no-op)+ if (+ posthog.__loaded &&+ !event.repeat &&+ !(hotkey.willHandleEvent && (event.metaKey || event.ctrlKey))+ ) {+ posthog.capture('keybind triggered', {+ keybind: formatTriggeredKeybind(event, normalizedKey),+ mechanism: 'hotkey',+ })+ }hotkey.action(event)break}