Advertisement
Guest User

Change NumLock LED On/Off when Kana Modifier is pressed

a guest
May 9th, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;
  2. ; AutoHotkey Version: 1.x
  3. ; Language:       English
  4. ; Platform:       Win9x/NT
  5. ; Author:         Random SuperUser Viewer
  6. ;
  7. ; Script Function:
  8. ;   AutoHotKey Script to turn the NumLock into  Kana Modifer Key function.
  9.  
  10. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  11. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  12. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  13.  
  14. ; Actual Hotkeys are at the bottom. Functions must be first. So scroll at the bottom!
  15.  
  16. ; Thanks to https://superuser.com/users/281372/ross-presser for linking it in a previous answer.
  17. ; Copy & Paste great code from the Internet: https://autohotkey.com/board/topic/9587-keyboard-led-control-capslocknumlockscrolllock-lights/
  18. KeyboardLED(LEDvalue, Cmd)  ; LEDvalue: ScrollLock=1, NumLock=2, CapsLock=4 ; Cmd = on/off/switch
  19. {
  20.   Static h_device
  21.   If ! h_device ; initialise
  22.     {
  23.     device =\Device\KeyBoardClass0
  24.     SetUnicodeStr(fn,device)
  25.     h_device:=NtCreateFile(fn,0+0x00000100+0x00000080+0x00100000,1,1,0x00000040+0x00000020,0)
  26.     }
  27.  
  28.   VarSetCapacity( output_actual, 4, 0 )
  29.   input_size = 4
  30.   VarSetCapacity( input, input_size, 0 )
  31.  
  32.   If Cmd= switch  ;switches every LED according to LEDvalue
  33.    KeyLED:= LEDvalue
  34.   If Cmd= on  ;forces all choosen LED's to ON (LEDvalue= 0 ->LED's according to keystate)
  35.    KeyLED:= LEDvalue | (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  36.   If Cmd= off  ;forces all choosen LED's to OFF (LEDvalue= 0 ->LED's according to keystate)
  37.     {
  38.     LEDvalue:= LEDvalue ^ 7
  39.     KeyLED:= LEDvalue & (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  40.     }
  41.   ; EncodeInteger( KeyLED, 1, &input, 2 ) ;input bit pattern (KeyLED): bit 0 = scrolllock ;bit 1 = numlock ;bit 2 = capslock
  42.   input := Chr(1) Chr(1) Chr(KeyLED)
  43.   input := Chr(1)
  44.   input=
  45.   success := DllCall( "DeviceIoControl"
  46.               , "uint", h_device
  47.               , "uint", CTL_CODE( 0x0000000b     ; FILE_DEVICE_KEYBOARD
  48.                         , 2
  49.                         , 0             ; METHOD_BUFFERED
  50.                         , 0  )          ; FILE_ANY_ACCESS
  51.               , "uint", &input
  52.               , "uint", input_size
  53.               , "uint", 0
  54.               , "uint", 0
  55.               , "uint", &output_actual
  56.               , "uint", 0 )
  57. }
  58.  
  59. CTL_CODE( p_device_type, p_function, p_method, p_access )
  60. {
  61.   Return, ( p_device_type << 16 ) | ( p_access << 14 ) | ( p_function << 2 ) | p_method
  62. }
  63.  
  64.  
  65. NtCreateFile(ByRef wfilename,desiredaccess,sharemode,createdist,flags,fattribs)
  66. {
  67.   VarSetCapacity(fh,4,0)
  68.   VarSetCapacity(objattrib,24,0)
  69.   VarSetCapacity(io,8,0)
  70.   VarSetCapacity(pus,8)
  71.   uslen:=DllCall("lstrlenW","str",wfilename)*2
  72.   InsertInteger(uslen,pus,0,2)
  73.   InsertInteger(uslen,pus,2,2)
  74.   InsertInteger(&wfilename,pus,4)
  75.   InsertInteger(24,objattrib,0)
  76.   InsertInteger(&pus,objattrib,8)
  77.   status:=DllCall("ntdll\ZwCreateFile","str",fh,"UInt",desiredaccess,"str",objattrib,"str",io,"UInt",0,"UInt",fattribs
  78.                   ,"UInt",sharemode,"UInt",createdist,"UInt",flags,"UInt",0,"UInt",0, "UInt")
  79.   return % ExtractInteger(fh)
  80. }
  81.  
  82.  
  83. SetUnicodeStr(ByRef out, str_)
  84. {
  85.   VarSetCapacity(st1, 8, 0)
  86.   InsertInteger(0x530025, st1)
  87.   VarSetCapacity(out, (StrLen(str_)+1)*2, 0)
  88.   DllCall("wsprintfW", "str", out, "str", st1, "str", str_, "Cdecl UInt")
  89. }
  90.  
  91.  
  92. ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)
  93. ; pSource is a string (buffer) whose memory area contains a raw/binary integer at pOffset.
  94. ; The caller should pass true for pSigned to interpret the result as signed vs. unsigned.
  95. ; pSize is the size of PSource's integer in bytes (e.g. 4 bytes for a DWORD or Int).
  96. ; pSource must be ByRef to avoid corruption during the formal-to-actual copying process
  97. ; (since pSource might contain valid data beyond its first binary zero).
  98. {
  99.   Loop %pSize%  ; Build the integer by adding up its bytes.
  100.     result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1)
  101.   if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
  102.     return result  ; Signed vs. unsigned doesn't matter in these cases.
  103.   ; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart:
  104.   return -(0xFFFFFFFF - result + 1)
  105. }
  106.  
  107.  
  108. InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
  109. ; The caller must ensure that pDest has sufficient capacity.  To preserve any existing contents in pDest,
  110. ; only pSize number of bytes starting at pOffset are altered in it.
  111. {
  112.   Loop %pSize%  ; Copy each byte in the integer into the structure as raw binary data.
  113.     DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
  114. }
  115.  
  116. ; NumpadLED is coded 2.
  117. KeyboardLED(2, "off")  ; Forces the light off. Perfect for starting up.
  118. sleep 100
  119. Kana_Romanji := false ; Saving the Pseudo-State, not sure how to read the actual mode.
  120.  
  121. ; Block commenting the Pure AutoHotKey Solution because the OP said he changed the NumLock key to Kana (VK_15) key.
  122. /*
  123. ; AutoHotKey now took control of the Kana Modifier (You could also use "NumLock" instead of the KanaModifier VK15)
  124. NumLock:: ; Change this to "VK15::" if your layout is using the key and delete the send {vk15} or comment this and uncomment the below one.
  125. Send {vk15} ; Actual Kana_Modifier key as given from MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
  126. sleep 10 ; Needs some delay because without delay Windows picks up the actual NumLock state and turns the light off. At least it did when I tried without it.
  127. if Kana_Romanji ;  swap the off and switch to swap the LED state for it. Currently: Romanji when it is on, Kana when it is off.
  128.     KeyboardLED(2, "off")  
  129. else
  130.     KeyboardLED(2, "switch")
  131. Kana_Romanji := not Kana_Romanji
  132. return
  133. */
  134. ; Block Comment Over
  135.  
  136.  
  137. ; Kana Modifier Key Version. There is a conflict if both are used at the same time. (NumLock calls this one?)
  138. ; Actual Kana Modifier key as given from MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
  139. vk15::
  140. if Kana_Romanji ;  swap the off and switch to swap the LED state for it. Currently: Romanji when it is on, Kana when it is off.
  141.     KeyboardLED(2, "off")  
  142. else
  143.     KeyboardLED(2, "switch")
  144. Kana_Romanji := not Kana_Romanji
  145. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement