Switch Off Num Lock

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Switch Off Num Lock

Post by IdeasVacuum »

This code switches Num Lock on:

Code: Select all

Global NumLockOn.INPUT
       NumLockOn\type = #INPUT_KEYBOARD
       NumLockOn\ki\wVk = #VK_NUMLOCK
       NumLockOn\ki\wScan = 144
       NumLockOn\ki\dwFlags = 0
       NumLockOn\ki\time = 0
       NumLockOn\ki\dwExtraInfo = 0

If(GetKeyState_(#VK_NUMLOCK) = #False) : SendInput_(1,@NumLockOn,SizeOf(INPUT)) : EndIf
This code however, does not switch Num Lock off:

Code: Select all

Global NumLockOff.INPUT
       NumLockOff\type = #INPUT_KEYBOARD
       NumLockOff\ki\wVk = #VK_NUMLOCK
       NumLockOff\ki\wScan = 144
       NumLockOff\ki\dwFlags = #KEYEVENTF_KEYUP
       NumLockOff\ki\time = 0
       NumLockOff\ki\dwExtraInfo = 0

If(GetKeyState_(#VK_NUMLOCK) = #True) : SendInput_(1,@NumLockOff,SizeOf(INPUT)) : EndIf
What am I doing wrong?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Switch Off Num Lock

Post by netmaestro »

You must press and release the key, as you would physically:

Code: Select all

Procedure NumLock_ON()
  Dim numlock.INPUT(1)
  
  With numlock(0)
    \type = #INPUT_KEYBOARD
    \ki\wVk = #VK_NUMLOCK
    \ki\wScan = 144
    \ki\dwFlags = 0
    \ki\time = 0
    \ki\dwExtraInfo = 0
  EndWith
  
  With numlock(1)
    \type = #INPUT_KEYBOARD
    \ki\wVk = #VK_NUMLOCK
    \ki\wScan = 144
    \ki\dwFlags = #KEYEVENTF_KEYUP
    \ki\time = 0
    \ki\dwExtraInfo = 0
  EndWith
  
  If Not GetKeyState_(#VK_NUMLOCK)
    SendInput_(2,@NumLock(),SizeOf(INPUT)) 
  EndIf
  
  ProcedureReturn
EndProcedure

Procedure NumLock_OFF()
  Dim numlock.INPUT(3)
  
  With numlock(0)
    \type = #INPUT_KEYBOARD
    \ki\wVk = #VK_NUMLOCK
    \ki\wScan = 144
    \ki\dwFlags = 0
    \ki\time = 0
    \ki\dwExtraInfo = 0
  EndWith
  
  With numlock(1)
    \type = #INPUT_KEYBOARD
    \ki\wVk = #VK_NUMLOCK
    \ki\wScan = 144
    \ki\dwFlags = #KEYEVENTF_KEYUP
    \ki\time = 0
    \ki\dwExtraInfo = 0
  EndWith
  
  If GetKeyState_(#VK_NUMLOCK)
    SendInput_(2,@NumLock(),SizeOf(INPUT)) 
  EndIf
  
  ProcedureReturn
EndProcedure


NumLock_ON()

Delay(3000)

NumLock_OFF()
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Switch Off Num Lock

Post by ts-soft »

This switches the numlock :mrgreen:

Code: Select all

Procedure SwitchNumlock()
  keybd_event_(#VK_NUMLOCK, 0, 0, 0) 
  keybd_event_(#VK_NUMLOCK, 0, #KEYEVENTF_KEYUP, 0)
EndProcedure

For i = 0 To 100
  SwitchNumlock()
  Delay(200)
Next
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Switch Off Num Lock

Post by netmaestro »

Deprecated :wink:
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Switch Off Num Lock

Post by IdeasVacuum »

Thanks netmaestro, I'll stick with the SendInput method.

Works really nicely, I think I'll just sit here for a while watching the LED toggle on/of.........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Switch Off Num Lock

Post by netmaestro »

Works really nicely, I think I'll just sit here for a while watching the LED toggle on/of.........
I bet it doesn't cost your family much to please you at Christmas :D
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Switch Off Num Lock

Post by IdeasVacuum »

..... could use the keyboard as an xmas tree light :idea:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Switch Off Num Lock

Post by MachineCode »

netmaestro wrote:Deprecated :wink:
Not until Microsoft removes it from the OS. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Switch Off Num Lock

Post by Shield »

Well it is officially marked as superseded by Microsoft.
So SendInput should be used.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Switch Off Num Lock

Post by IdeasVacuum »

Thanks for your tip too TS-Soft, didn't spot it was yours yesterday because for some strange reason netmaestro's frog image replaced yours........ :shock:

Looks as though MS are finally moving away from some of the older API. You can often see why they would want to, but sometimes the replacement could have been better designed.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply