Page 1 of 1
Keyboard NumLock and CapsLock LED on/off under Win
Posted: Mon Apr 01, 2013 4:05 pm
by Simo_na
Hi, please help me...
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)
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Mon Apr 01, 2013 5:08 pm
by IdeasVacuum
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Mon Apr 01, 2013 6:30 pm
by Simo_na
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 ?
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 12:10 am
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.
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 3:19 am
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.
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 2:21 pm
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.
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 2:29 pm
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 
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 2:53 pm
by IdeasVacuum
...but note, it is better to use the SendInput() method and although it requires more lines of code, it is straightforward stuff.
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 3:02 pm
by Simo_na
This work !
EDIT
@IdeasVacuum
the code indicated by MachineCode seems work fine in my case.
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 6:29 pm
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?
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Tue Apr 02, 2013 7:28 pm
by NabDa

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Ă !
Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Wed Apr 03, 2013 12:13 am
by Simo_na
NabDa Cool!
any way to return the state of the #VK's ??
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 :

Re: Keyboard NumLock and CapsLock LED on/off under Win
Posted: Wed Apr 03, 2013 3:27 am
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...
