Setting numlock, caps lock

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

I have problems with the above code.

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
Translated from Powerbasic (hope no mistake !)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re:

Post by PB »

My code above didn't survive a forum migration properly.

This line...

Code: Select all

If newstateoldstate
...is supposed to be like this:

Code: Select all

If newstate<>oldstate
Just for the record. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Setting numlock, caps lock

Post by Fred »

Updated :)
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Setting numlock, caps lock

Post by electrochrisso »

Is gnozal code more reliable (work better), or are both versions just as good. :?:
PureBasic! Purely the best 8)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Setting numlock, caps lock

Post by PB »

My original code above needs further fixing, because it doesn't
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
Perhaps Fred can amend my original post once more. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Setting numlock, caps lock

Post by RASHAD »

Hi
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)
Edit :Modified to activate certain state only
Last edited by RASHAD on Tue Jun 17, 2014 7:53 am, edited 1 time in total.
Egypt my love
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Setting numlock, caps lock

Post by electrochrisso »

Thanks PB, I have now changed your code in my archives. :)
Hey RASHAD you had to complicate things even further. :lol:
PureBasic! Purely the best 8)
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Setting numlock, caps lock

Post by Demivec »

@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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Setting numlock, caps lock

Post by RASHAD »

Hi electrochrisso
Not to complicate things
keybd_event_() is depreciated as per MSDN

Hi Demivec
Previous post updated
Egypt my love
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Setting numlock, caps lock

Post by Demivec »

RASHAD wrote:Previous post updated
@RASHAD: Thanks. :)
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Setting numlock, caps lock

Post by electrochrisso »

RASHAD wrote:Hi electrochrisso
Not to complicate things
keybd_event_() is depreciated as per MSDN

Hi Demivec
Previous post updated
Thanks for the info, you are still the number 1 API guru. :)
PureBasic! Purely the best 8)
Post Reply