import{useCallback,useEffect,useLayoutEffect,useRef}from"react";function getOS(){let e=window.navigator.userAgent.toLowerCase(),t=null;return/(macintosh|macintel|macppc|mac68k|macos)/i.test(e)?t="macos":/(iphone|ipad|ipod)/i.test(e)?t="ios":/(win32|win64|windows|wince)/i.test(e)?t="windows":/android/.test(e)?t="android":!t&&/linux/.test(e)&&(t="linux"),t}const useKeyPress=(e,t=!1,s,n)=>{const o=getOS(),r=useRef(s);useLayoutEffect((()=>{r.current=s}));const a=useCallback((s=>{e.some((e=>s.key===e))&&(t?"ctrl"===t&&(s.ctrlKey||"macos"===o&&s.metaKey)?(1===e.length&&"s"===e[0]&&s.preventDefault(),r.current(s)):("shift"===t&&s.shiftKey||"alt"===t&&s.altKey||"meta"===t&&s.metaKey)&&r.current(s):r.current(s))}),[e]);useEffect((()=>{const e=n??document;return e&&e.addEventListener("keydown",a),()=>e&&e.removeEventListener("keydown",a)}),[a,n])};export default useKeyPress;