1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
   |  
   1. ' Declare Type for API call:
   2.       Private Type OSVERSIONINFO
   3.         dwOSVersionInfoSize As Long
   4.         dwMajorVersion As Long
   5.         dwMinorVersion As Long
   6.         dwBuildNumber As Long
   7.         dwPlatformId As Long
   8.         szCSDVersion As String * 128   '  Maintenance string for PSS usage
   9.       End Type
  10.
  11.       ' API declarations:
  12.
  13.       Private Declare Function GetVersionEx Lib "kernel32" _
  14.          Alias "GetVersionExA" _
  15.          (lpVersionInformation As OSVERSIONINFO) As Long
  16.
  17.       Private Declare Sub keybd_event Lib "user32" _
  18.          (ByVal bVk As Byte, _
  19.           ByVal bScan As Byte, _
  20.           ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  21.
  22.       Private Declare Function GetKeyboardState Lib "user32" _
  23.          (pbKeyState As Byte) As Long
  24.
  25.       Private Declare Function SetKeyboardState Lib "user32" _
  26.          (lppbKeyState As Byte) As Long
  27.
  28.       ' Constant declarations:
  29.       Const VK_NUMLOCK = &H90
  30.       Const VK_SCROLL = &H91
  31.       Const VK_CAPITAL = &H14
  32.       Const KEYEVENTF_EXTENDEDKEY = &H1
  33.       Const KEYEVENTF_KEYUP = &H2
  34.       Const VER_PLATFORM_WIN32_NT = 2
  35.       Const VER_PLATFORM_WIN32_WINDOWS = 1
  36.
  37. Private Sub Command1_Click()
  38.       Dim o As OSVERSIONINFO
  39.       Dim NumLockState As Boolean
  40.       Dim ScrollLockState As Boolean
  41.       Dim CapsLockState As Boolean
  42.
  43.       o.dwOSVersionInfoSize = Len(o)
  44.       GetVersionEx o
  45.       Dim keys(0 To 255) As Byte
  46.       GetKeyboardState keys(0)
  47.
  48.       ' NumLock handling:
  49.       NumLockState = keys(VK_NUMLOCK)
  50.       If NumLockState <> True Then    'Turn numlock on
  51.         If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98
  52.
  53.           keys(VK_NUMLOCK) = 1
  54.           SetKeyboardState keys(0)
  55.         ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT
  56.         'Simulate Key Press
  57.           keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
  58.         'Simulate Key Release
  59.           keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
  60.              Or KEYEVENTF_KEYUP, 0
  61.         End If
  62.       End If
  63.
  64.       ' CapsLock handling:
  65.       CapsLockState = keys(VK_CAPITAL)
  66.       If CapsLockState <> True Then    'Turn capslock on
  67.         If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98
  68.           keys(VK_CAPITAL) = 1
  69.           SetKeyboardState keys(0)
  70.         ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT
  71.         'Simulate Key Press
  72.           keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
  73.         'Simulate Key Release
  74.           keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
  75.              Or KEYEVENTF_KEYUP, 0
  76.         End If
  77.       End If
  78.
  79.       ' ScrollLock handling:
  80.       ScrollLockState = keys(VK_SCROLL)
  81.       If ScrollLockState <> True Then    'Turn Scroll lock on
  82.         If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98
  83.           keys(VK_SCROLL) = 1
  84.           SetKeyboardState keys(0)
  85.         ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT
  86.         'Simulate Key Press
  87.           keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
  88.         'Simulate Key Release
  89.           keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _
  90.             Or KEYEVENTF_KEYUP, 0
  91.         End If
  92.       End If
  93.     End Sub  | 
Partager