Advertisement
Guest User

CM Storm Devastator keyboard Backlight LED Control

a guest
Aug 7th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. ; Keyboard LED control (capslock/numlock/scrolllock lights)
  2. ; http://www.autohotkey.com/forum/viewtopic.php?t=10532
  3.  
  4. ; USAGE: KeyboardLED(LEDvalue,"Cmd") ; LEDvalue: ScrollLock=1, NumLock=2, CapsLock=4 ; Cmd = on/off/switch
  5.  
  6. ~ScrollLock::
  7. { if GetKeyState("CapsLock", "T")
  8. KeyboardLED(4,"on")
  9. else
  10. KeyboardLED(4,"off")
  11. if GetKeyState("NumLock", "T")
  12. KeyboardLED(2,"on")
  13. else
  14. KeyboardLED(2,"off")
  15. KeyboardLED(1,"on")
  16. }
  17. ~NumLock::
  18. { if GetKeyState("CapsLock", "T")
  19. KeyboardLED(4,"on")
  20. else
  21. KeyboardLED(4,"off")
  22. if GetKeyState("NumLock", "T")
  23. KeyboardLED(2,"on")
  24. else
  25. KeyboardLED(2,"off")
  26. KeyboardLED(1,"on")
  27. }
  28. ~CapsLock::
  29. { if GetKeyState("CapsLock", "T")
  30. KeyboardLED(4,"on")
  31. else
  32. KeyboardLED(4,"off")
  33. if GetKeyState("NumLock", "T")
  34. KeyboardLED(2,"on")
  35. else
  36. KeyboardLED(2,"off")
  37. KeyboardLED(1,"on")
  38. }
  39. Return
  40.  
  41.  
  42. KeyboardLED(LEDvalue, Cmd) ; LEDvalue: ScrollLock=1, NumLock=2, CapsLock=4 ; Cmd = on/off/switch
  43. {
  44. Static h_device
  45. If ! h_device ; initialise
  46. {
  47. device =\Device\KeyBoardClass0
  48. SetUnicodeStr(fn,device)
  49. h_device:=NtCreateFile(fn,0+0x00000100+0x00000080+0x00100000,1,1,0x00000040+0x00000020,0)
  50. }
  51.  
  52. VarSetCapacity( output_actual, 4, 0 )
  53. input_size = 4
  54. VarSetCapacity( input, input_size, 0 )
  55.  
  56. If Cmd= switch ;switches every LED according to LEDvalue
  57. KeyLED:= LEDvalue
  58. If Cmd= on ;forces all choosen LED's to ON (LEDvalue= 0 ->LED's according to keystate)
  59. KeyLED:= LEDvalue | (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  60. If Cmd= off ;forces all choosen LED's to OFF (LEDvalue= 0 ->LED's according to keystate)
  61. {
  62. LEDvalue:= LEDvalue ^ 7
  63. KeyLED:= LEDvalue & (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  64. }
  65. ; EncodeInteger( KeyLED, 1, &input, 2 ) ;input bit pattern (KeyLED): bit 0 = scrolllock ;bit 1 = numlock ;bit 2 = capslock
  66. input := Chr(1) Chr(1) Chr(KeyLED)
  67. input := Chr(1)
  68. input=
  69. success := DllCall( "DeviceIoControl"
  70. , "uint", h_device
  71. , "uint", CTL_CODE( 0x0000000b ; FILE_DEVICE_KEYBOARD
  72. , 2
  73. , 0 ; METHOD_BUFFERED
  74. , 0 ) ; FILE_ANY_ACCESS
  75. , "uint", &input
  76. , "uint", input_size
  77. , "uint", 0
  78. , "uint", 0
  79. , "uint", &output_actual
  80. , "uint", 0 )
  81. }
  82.  
  83. CTL_CODE( p_device_type, p_function, p_method, p_access )
  84. {
  85. Return, ( p_device_type << 16 ) | ( p_access << 14 ) | ( p_function << 2 ) | p_method
  86. }
  87.  
  88.  
  89. NtCreateFile(ByRef wfilename,desiredaccess,sharemode,createdist,flags,fattribs)
  90. {
  91. VarSetCapacity(fh,4,0)
  92. VarSetCapacity(objattrib,24,0)
  93. VarSetCapacity(io,8,0)
  94. VarSetCapacity(pus,8)
  95. uslen:=DllCall("lstrlenW","str",wfilename)*2
  96. InsertInteger(uslen,pus,0,2)
  97. InsertInteger(uslen,pus,2,2)
  98. InsertInteger(&wfilename,pus,4)
  99. InsertInteger(24,objattrib,0)
  100. InsertInteger(&pus,objattrib,8)
  101. status:=DllCall("ntdll\ZwCreateFile","str",fh,"UInt",desiredaccess,"str",objattrib,"str",io,"UInt",0,"UInt",fattribs
  102. ,"UInt",sharemode,"UInt",createdist,"UInt",flags,"UInt",0,"UInt",0, "UInt")
  103. return % ExtractInteger(fh)
  104. }
  105.  
  106.  
  107. SetUnicodeStr(ByRef out, str_)
  108. {
  109. VarSetCapacity(st1, 8, 0)
  110. InsertInteger(0x530025, st1)
  111. VarSetCapacity(out, (StrLen(str_)+1)*2, 0)
  112. DllCall("wsprintfW", "str", out, "str", st1, "str", str_, "Cdecl UInt")
  113. }
  114.  
  115.  
  116. ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)
  117. ; pSource is a string (buffer) whose memory area contains a raw/binary integer at pOffset.
  118. ; The caller should pass true for pSigned to interpret the result as signed vs. unsigned.
  119. ; pSize is the size of PSource's integer in bytes (e.g. 4 bytes for a DWORD or Int).
  120. ; pSource must be ByRef to avoid corruption during the formal-to-actual copying process
  121. ; (since pSource might contain valid data beyond its first binary zero).
  122. {
  123. Loop %pSize% ; Build the integer by adding up its bytes.
  124. result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1)
  125. if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
  126. return result ; Signed vs. unsigned doesn't matter in these cases.
  127. ; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart:
  128. return -(0xFFFFFFFF - result + 1)
  129. }
  130.  
  131.  
  132. InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
  133. ; The caller must ensure that pDest has sufficient capacity. To preserve any existing contents in pDest,
  134. ; only pSize number of bytes starting at pOffset are altered in it.
  135. {
  136. Loop %pSize% ; Copy each byte in the integer into the structure as raw binary data.
  137. DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement