Keyboard NumLock and CapsLock LED on/off under Win

Just starting out? Need help? Post your questions and find answers here.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

Hi, please help me... :) :oops:
Keyboard : how to turn ON or OFF the NumLock and CapsLock LED via Purebasic ?


i've found this code and it work but is for Blitzmax :(

Code: Select all

Extern "Win32"
Function keybd_event (bVk:Int, bScan:Int, dwFlags:Int, dwExtraInfo:Int) 
End Extern
Const VK_CAPITAL = $14
Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY Or 0, 0)
Last edited by Simo_na on Mon Apr 01, 2013 6:41 pm, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

IdeasVacuum wrote:netmaestro has the answer here: http://www.purebasic.fr/english/viewtop ... =+num+lock

thank you ! :)
saw....
a code very long compared to this win32 call ....and this call works fine...
...but one method in purebasic to use the win32 api ?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by luis »

It's natively supported, just call it: keybd_event_

FYI this API is deprecated, SendInput() (used in the thread linked) is now the recommended way.
"Have you tried turning it off and on again ?"
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

luis wrote:It's natively supported, just call it: keybd_event_

FYI this API is deprecated, SendInput() (used in the thread linked) is now the recommended way.
Thanks , but with this code active

Code: Select all

Procedure record_CallBack(hWnd.i,Msg.i,wParam.i,lParam.i)
If Msg=#MM_WIM_DATA
record_Read (wParam,lParam):record_doFFT (Config\LScope)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
...don't work :(

i want to send an audio clip @ Bloc Num Led on my keyboard.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by IdeasVacuum »

...don't work
That is really a seperate subject and should really be a seperate post. The code itself, isolated, means very little, so to get help you need to expand your description of what exactly it is that you are trying to do.
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: Keyboard NumLock and CapsLock LED on/off under Win

Post by MachineCode »

Simo_na wrote:Keyboard : how to turn ON or OFF the NumLock and CapsLock LED via Purebasic ?
Search the forums, son -> http://www.purebasic.fr/english/viewtop ... 968#p34968 ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by IdeasVacuum »

...but note, it is better to use the SendInput() method and although it requires more lines of code, it is straightforward stuff.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

MachineCode wrote:
Simo_na wrote:Keyboard : how to turn ON or OFF the NumLock and CapsLock LED via Purebasic ?
Search the forums, son -> http://www.purebasic.fr/english/viewtop ... 968#p34968 ;)

This work ! :D

EDIT
@IdeasVacuum

the code indicated by MachineCode seems work fine in my case.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by IdeasVacuum »

the code indicated by MachineCode seems work fine in my case.
Indeed, but the API function keybd_event is deprecated, which means that at some time in the future, it will not be available for use and therefore it won't work. When that will actually happen, who knows, but if Microsoft have already defined the replacement (which is to use SendInput), then why not use it?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
NabDa
New User
New User
Posts: 3
Joined: Fri Sep 14, 2012 3:58 pm
Location: France

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by NabDa »

:D Hello !

Maybe these few lines of code may help you:

Code: Select all

;
; SendInput API fonction example
;

Define evnt.INPUT

evnt\type = #INPUT_KEYBOARD
evnt\ki\wVk = #KEYEVENTF_EXTENDEDKEY
evnt\ki\wScan = 0; // hardware scan code for key
evnt\ki\time = 0;
evnt\ki\dwExtraInfo = 0;

; simulate the caps/or num lock key
evnt\type = #INPUT_KEYBOARD;
evnt\ki\wVk = #VK_NUMLOCK; // virtual-key code for the num-lock key (toggle NUMLOCK)
evnt\ki\dwFlags = 0; // 0 for key press
SendInput_(1, @evnt, SizeOf(INPUT));

;simulate key release
evnt\type = #INPUT_KEYBOARD;
evnt\ki\dwFlags = #KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput_(1, @evnt, SizeOf(INPUT));
to toggle CAPSLOCK, just replace:

Code: Select all

evnt\ki\wVk = #VK_NUMLOCK; // virtual-key code for the num lock key (toggle NUMLOCK)
with:

Code: Select all

evnt\ki\wVk = #VK_CAPITAL; // virtual-key code for the caps-lock key (toggle CAPSLOCK)
Voilà !
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

NabDa Cool!

any way to return the state of the #VK's ??

:D

Code: Select all


;
; SendInput API fonction example
;
EnableExplicit
Procedure mykey(vk) 
Define evnt.INPUT
evnt\type = #INPUT_KEYBOARD
evnt\ki\wVk = #KEYEVENTF_EXTENDEDKEY
evnt\ki\wScan = 0; // hardware scan code for key
evnt\ki\time = 0;
evnt\ki\dwExtraInfo = 0;
; simulate the caps/or num lock key
evnt\type = #INPUT_KEYBOARD;
If vk=1 : evnt\ki\wVk = #VK_NUMLOCK:EndIf  ; // virtual-key code for the num-lock key (toggle NUMLOCK) 
If vk=2 : evnt\ki\wVk = #VK_CAPITAL:EndIf  ; // virtual-key code for the num-lock key (toggle CAPITAL) 
If vk=3 : evnt\ki\wVk = #VK_SCROLL:EndIf  ; // virtual-key code for the num-lock key (toggle SCROLL) 
evnt\ki\dwFlags = 0; // 0 for key press
SendInput_(1, @evnt, SizeOf(INPUT));
;simulate key release
evnt\type = #INPUT_KEYBOARD;
evnt\ki\dwFlags = #KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput_(1, @evnt, SizeOf(INPUT));
EndProcedure

mykey(1) ; (toggle NUMLOCK) 
mykey(2) ; (toggle CAPITAL) 
mykey(3) ; (toggle SCROLL) 

IdeasVacuum : :)
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: Keyboard NumLock and CapsLock LED on/off under Win

Post by Simo_na »

Code: Select all

;-------------------------------
; Keyboard Led Handling For Win
;-------------------------------
; v 1.0 - V 1.01
;-------------------------------

EnableExplicit

Global re1,re2,re3 ; read initial state
Global wr1,wr2,wr3 ; read final state

Procedure myinit() ; V1.01 update
re1= GetKeyState_(#VK_NUMLOCK) ;get #VK_NUMLOCK @ start
re2= GetKeyState_(#VK_CAPITAL) ;get #VK_CAPITAL @ start
re3= GetKeyState_(#VK_SCROLL)  ;get #VK_CAPITAL @ start
EndProcedure

Procedure mykey(vk)
Define evnt.INPUT
evnt\type = #INPUT_KEYBOARD
evnt\ki\wVk = #KEYEVENTF_EXTENDEDKEY
evnt\ki\wScan = 0; // hardware scan code for key
evnt\ki\time = 0;
evnt\ki\dwExtraInfo = 0;
; simulate the caps/or num lock key
evnt\type = #INPUT_KEYBOARD;
If vk=1 : evnt\ki\wVk = #VK_NUMLOCK:EndIf  ; // virtual-key code for the num-lock key (toggle NUMLOCK)
If vk=2 : evnt\ki\wVk = #VK_CAPITAL:EndIf  ; // virtual-key code for the num-lock key (toggle CAPITAL)
If vk=3 : evnt\ki\wVk = #VK_SCROLL:EndIf  ; // virtual-key code for the num-lock key (toggle SCROLL)
evnt\ki\dwFlags = 0; // 0 for key press
SendInput_(1, @evnt, SizeOf(INPUT));
;simulate key release
evnt\type = #INPUT_KEYBOARD;
evnt\ki\dwFlags = #KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput_(1, @evnt, SizeOf(INPUT));
EndProcedure

Procedure myreset() ; --- this is for only #VK_CAPITAL ---
wr1= GetKeyState_(#VK_NUMLOCK) ; get #VK_NUMLOCK @ end
wr2= GetKeyState_(#VK_CAPITAL) ; get #VK_CAPITAL @ end
wr3= GetKeyState_(#VK_SCROLL)  ; get #VK_SCROLL  @ end
;---reset #VK_CAPITAL
If re2<>wr2 ; compare re2 and wr2
mykey(2) ; if not equal simulate again...
EndIf
;---
EndProcedure

; --- TEST ---
myinit() ;read the initial state - V1.01 update
mykey(2) ; toggle CAPITAL
Delay (500) ;for verify
myreset() ; reset to initial state.
EDIT
updated to V 1.01

EDIT
From keyboard LED...to external world... :D

Image
Post Reply