Page 1 of 1

Posted: Wed Mar 27, 2002 10:04 pm
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

Posted: Wed Mar 27, 2002 10:51 pm
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.

Posted: Fri Sep 26, 2003 2:12 pm
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 !)

Re:

Posted: Sun Jun 15, 2014 8:51 am
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. ;)

Re: Setting numlock, caps lock

Posted: Sun Jun 15, 2014 9:55 am
by Fred
Updated :)

Re: Setting numlock, caps lock

Posted: Mon Jun 16, 2014 1:25 am
by electrochrisso
Is gnozal code more reliable (work better), or are both versions just as good. :?:

Re: Setting numlock, caps lock

Posted: Mon Jun 16, 2014 8:29 am
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. ;)

Re: Setting numlock, caps lock

Posted: Mon Jun 16, 2014 9:27 am
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

Re: Setting numlock, caps lock

Posted: Tue Jun 17, 2014 2:45 am
by electrochrisso
Thanks PB, I have now changed your code in my archives. :)
Hey RASHAD you had to complicate things even further. :lol:

Re: Setting numlock, caps lock

Posted: Tue Jun 17, 2014 5:38 am
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.

Re: Setting numlock, caps lock

Posted: Tue Jun 17, 2014 7:57 am
by RASHAD
Hi electrochrisso
Not to complicate things
keybd_event_() is depreciated as per MSDN

Hi Demivec
Previous post updated

Re: Setting numlock, caps lock

Posted: Tue Jun 17, 2014 8:31 am
by Demivec
RASHAD wrote:Previous post updated
@RASHAD: Thanks. :)

Re: Setting numlock, caps lock

Posted: Thu Jun 19, 2014 12:53 am
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. :)