Setting numlock, caps lock
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by cor.
Is it possible, to set in background the numlock, capslock to on/off
even if the program is not in foreground.
Using Windows 98 SE
Registered PB version : 2.90 (Windows)
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
Is it possible, to set in background the numlock, capslock to on/off
even if the program is not in foreground.
Using Windows 98 SE
Registered PB version : 2.90 (Windows)
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Code: Select all
; Set state of LED keys (Caps Lock, Num Lock, Scroll Lock) by PB.
; Doesn't seem to work reliably on Windows 95.
Procedure SetLEDKey(key$,newstate)
Select LCase(key$)
Case "c" : keycode=#VK_CAPITAL : oldstate=GetKeyState_(keycode)
Case "n" : keycode=#VK_NUMLOCK : oldstate=GetKeyState_(keycode)
Case "s" : keycode=#VK_SCROLL : oldstate=GetKeyState_(keycode)
EndSelect
If newstate <> oldstate
keybd_event_(keycode,0,0,0) : keybd_event_(keycode,0,#KEYEVENTF_KEYUP,0)
EndIf
EndProcedure
SetLEDKey("c",1) ; Caps Lock on.
SetLEDKey("n",1) ; Num Lock on.
SetLEDKey("s",1) ; Scroll Lock on.
-
- PureBasic Expert
- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
I have problems with the above code.
This this seems to work better :
Translated from Powerbasic (hope no mistake !)
This this seems to work better :
Code: Select all
Procedure SetScrollLED(VKkey.l, bState.b)
;Turn on And off caps lock, scroll lock And num lock
;VKkey = #VK_CAPITAL, #VK_SCROLL, #VK_NUMLOCK
;bState = #TRUE For on, #FALSE For off
Dim keyState.b(256)
GetKeyboardState_(@keyState(0))
If (bState = #True And keyState(VKkey) = 0) Or (bState = #False And keyState(VKkey) = 1)
keybd_event_(VKkey, 0, #KEYEVENTF_EXTENDEDKEY, 0)
keybd_event_(VKkey, 0, #KEYEVENTF_EXTENDEDKEY + #KEYEVENTF_KEYUP, 0)
keyState(VKkey) = bState
SetKeyboardState_(@keyState(0))
EndIf
EndProcedure
Re:
My code above didn't survive a forum migration properly.
This line...
...is supposed to be like this:
Just for the record. 
This line...
Code: Select all
If newstateoldstate
Code: Select all
If newstate<>oldstate

I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: Setting numlock, caps lock
Updated 

- electrochrisso
- Addict
- Posts: 989
- Joined: Mon May 14, 2007 2:13 am
- Location: Darling River
Re: Setting numlock, caps lock
Is gnozal code more reliable (work better), or are both versions just as good. 

PureBasic! Purely the best 

Re: Setting numlock, caps lock
My original code above needs further fixing, because it doesn't
test correctly for the right bit state. Here's the corrected lines:
Perhaps Fred can amend my original post once more. 
test correctly for the right bit state. Here's the corrected lines:
Code: Select all
Case "c" : keycode=#VK_CAPITAL : oldstate=GetKeyState_(keycode) & $1
Case "n" : keycode=#VK_NUMLOCK : oldstate=GetKeyState_(keycode) & $1
Case "s" : keycode=#VK_SCROLL : oldstate=GetKeyState_(keycode) & $1

I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: Setting numlock, caps lock
Hi
On = 1
Off = 0
Edit :Modified to activate certain state only
On = 1
Off = 0
Code: Select all
;*************************** Num Lock *******************
Procedure NumLock(Flag)
If GetKeyState_(#VK_NUMLOCK) <> Flag
n.INPUT
n\type = #INPUT_KEYBOARD
n\ki\wVk = #VK_NUMLOCK
n\ki\dwFlags = 0
SendInput_(1,@n,SizeOf(n))
n\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1,@n,SizeOf(n))
EndIf
EndProcedure
NumLock(0)
;************************ Caps Lock **********************
Procedure CapsLock(Flag)
If GetKeyState_(#VK_CAPITAL) <> Flag
n.INPUT
n\type = #INPUT_KEYBOARD
n\ki\wVk = #VK_CAPITAL
n\ki\dwFlags = 0
SendInput_(1,@n,SizeOf(n))
n\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1,@n,SizeOf(n))
EndIf
EndProcedure
CapsLock(0)
;************************ Scroll Lock **********************
Procedure ScrollLock(Flag)
If GetKeyState_(#VK_SCROLL) <> Flag
n.INPUT
n\type = #INPUT_KEYBOARD
n\ki\wVk = #VK_SCROLL
n\ki\dwFlags = 0
SendInput_(1,@n,SizeOf(n))
n\ki\dwFlags = #KEYEVENTF_KEYUP
SendInput_(1,@n,SizeOf(n))
EndIf
EndProcedure
ScrollLock(0)
Last edited by RASHAD on Tue Jun 17, 2014 7:53 am, edited 1 time in total.
Egypt my love
- electrochrisso
- Addict
- Posts: 989
- Joined: Mon May 14, 2007 2:13 am
- Location: Darling River
Re: Setting numlock, caps lock
Thanks PB, I have now changed your code in my archives.
Hey RASHAD you had to complicate things even further.

Hey RASHAD you had to complicate things even further.

PureBasic! Purely the best 

Re: Setting numlock, caps lock
@RASHAD: Your code just seems to just toggle the state for Num Lock, Scroll Lock and Caps Lock. Would you be willing to adjust it to take a parameter to set a certain state as well?
Perhaps NumLock(state = -1) where it is set to On if state = 1, Off if state = 0, and simply toggled for the default and any other state.
Perhaps NumLock(state = -1) where it is set to On if state = 1, Off if state = 0, and simply toggled for the default and any other state.
Re: Setting numlock, caps lock
Hi electrochrisso
Not to complicate things
keybd_event_() is depreciated as per MSDN
Hi Demivec
Previous post updated
Not to complicate things
keybd_event_() is depreciated as per MSDN
Hi Demivec
Previous post updated
Egypt my love
Re: Setting numlock, caps lock
@RASHAD: Thanks.RASHAD wrote:Previous post updated

- electrochrisso
- Addict
- Posts: 989
- Joined: Mon May 14, 2007 2:13 am
- Location: Darling River
Re: Setting numlock, caps lock
Thanks for the info, you are still the number 1 API guru.RASHAD wrote:Hi electrochrisso
Not to complicate things
keybd_event_() is depreciated as per MSDN
Hi Demivec
Previous post updated

PureBasic! Purely the best 
