kiltec

InputExtension.cs

Jan 14th, 2023 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System.Runtime.InteropServices;
  2. using UnityEngine;
  3.  
  4. public class KeyExtension
  5. {
  6.  
  7. #if PLATFORM_STANDALONE_WIN
  8.  
  9.     [DllImport("user32.dll")]
  10.     public static extern short GetKeyState(int key);
  11.  
  12.     const int VK_CAPITAL = 0x14;
  13.     const int VK_NUMLOCK = 0x90;
  14.     const int VK_SCROLL = 0x91;
  15.  
  16.     public static bool IsCapsLockOn()
  17.     {
  18.         return (GetKeyState(VK_CAPITAL) & 0xffff) != 0;
  19.     }
  20.  
  21.     public static bool IsNumLockOn()
  22.     {
  23.         return (GetKeyState(VK_NUMLOCK) & 0xffff) != 0;
  24.     }
  25.  
  26.     public static bool IsScrollLockOn()
  27.     {
  28.         return (GetKeyState(VK_SCROLL) & 0xffff) != 0;
  29.     }
  30.  
  31. #else
  32.    
  33.     public static bool IsCapsLockOn()
  34.     {
  35.         return KeyValue(KeyCode.CapsLock);
  36.     }
  37.    
  38.     public static bool IsNumLockOn()
  39.     {
  40.         return KeyValue(KeyCode.Numlock);
  41.     }
  42.  
  43.     public static bool IsScrollLockOn()
  44.     {
  45.         return KeyValue(KeyCode.ScrollLock);
  46.     }
  47.  
  48. #endif
  49.  
  50.     public static bool KeyValue(KeyCode keyCode)
  51.     {
  52.         if (Application.isPlaying)
  53.         {
  54.             return (Input.GetKey(keyCode));
  55.         }
  56.         else
  57.         {
  58.             return (Event.current.keyCode == keyCode);
  59.         }
  60.     }
  61.  
  62. }
Tags: Unity input
Advertisement
Add Comment
Please, Sign In to add comment