lenix 1.7.22 → 1.7.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/nui/focus.ts CHANGED
@@ -2,26 +2,15 @@ import { Request } from '../shared/types.ts'
2
2
  import { fetchNui } from './fetch.ts'
3
3
 
4
4
  /**
5
- * Requests keyboard and cursor focus for the NUI browser.
5
+ * Sets the keyboard and cursor focus state for the NUI browser.
6
6
  */
7
- export const focus = (exclude?: 'keyboard' | 'cursor'): Promise<true> => {
8
- return fetchNui<Request<true, '__nuiFocus', {
9
- keyboard: boolean,
10
- cursor: boolean
11
- }>>('__nuiFocus', { keyboard: exclude === 'keyboard' ? false : true, cursor: exclude === 'cursor' ? false : true })
12
- }
7
+ export const setFocus = (
8
+ keyboard: boolean,
9
+ cursor: boolean
10
+ ) => fetchNui<Request<true, '__nuiFocus', {
11
+ keyboard: boolean,
12
+ cursor: boolean
13
+ }>>('__nuiFocus', { keyboard, cursor })
13
14
 
14
- export const unFocus = (
15
- key: KeyboardEvent['key'],
16
- onEvent: () => void
17
- ) => {
18
-
19
- const handler = (e: KeyboardEvent) => {
20
- if (e.key !== key) return
21
-
22
- onEvent()
23
- }
24
-
25
- window.addEventListener('keydown', handler)
26
- return () => window.removeEventListener('keydown', handler)
27
- }
15
+ export const focus = () => setFocus(true, true);
16
+ export const unFocus = () => setFocus(false, false);
package/nui/index.ts CHANGED
@@ -8,3 +8,4 @@
8
8
  export * from './on.ts'
9
9
  export * from './fetch.ts'
10
10
  export * from './focus.ts'
11
+ export * from './keydown.ts'
package/nui/keydown.ts ADDED
@@ -0,0 +1,22 @@
1
+ const keyHandlers = new Map<string, Set<() => void>>()
2
+
3
+ window.addEventListener("keydown", (event) => {
4
+ keyHandlers.get(event.key)?.forEach((handler) => handler())
5
+ })
6
+
7
+ export const onKeyDown = (
8
+ key: KeyboardEvent["key"],
9
+ handler: () => void
10
+ ) => {
11
+ const handlers = keyHandlers.get(key) ?? new Set()
12
+ handlers.add(handler)
13
+ keyHandlers.set(key, handlers)
14
+
15
+ return () => {
16
+ handlers.delete(handler)
17
+
18
+ if (handlers.size === 0) {
19
+ keyHandlers.delete(key)
20
+ }
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lenix",
3
- "version": "1.7.22",
3
+ "version": "1.7.24",
4
4
  "description": "Typed FiveM utilities for client, server, shared, and NUI scripts",
5
5
  "author": "Lenix",
6
6
  "license": "MIT",