Detecting, setting state of caps-lock key

Hi everyone,

I have a barcode scanner connected to my application by USB. It reads the barcodes in a textfield control. It just read the barcode as keyboard presses.

So when using an AZERTY keyboard where the numbers are on top and the characters below, you get the characters (without pressing shift).

Now when the scanner scans a code into the textfield, I want the capslock key be “on” and after the barcode is scanned back off.

How can I get the state of the capslock key?

When it is not active, make it active (pressed)?

After the barcode is read then make it inactive again?

I searched for keyboard but can only find keyboard.shiftkey.

So my question comes down to : how can I check the state of the “capslock” key and how can I press/depress this key in code?

Thank you very much for your help.

Chris

[quote=198575:@Chris Verberne]Hi everyone,

I have a barcode scanner connected to my application by USB. It reads the barcodes in a textfield control. It just read the barcode as keyboard presses.

So when using an AZERTY keyboard where the numbers are on top and the characters below, you get the characters (without pressing shift).

Now when the scanner scans a code into the textfield, I want the capslock key be “on” and after the barcode is scanned back off.

How can I get the state of the capslock key?

When it is not active, make it active (pressed)?

After the barcode is read then make it inactive again?

I searched for keyboard but can only find keyboard.shiftkey.

So my question comes down to : how can I check the state of the “capslock” key and how can I press/depress this key in code?

Thank you very much for your help.

Chris[/quote]

PC I suppose ?

According to this
http://stackoverflow.com/questions/3955572/how-to-turn-on-caps-lock-whenever-rich-text-box-has-focus/3955645#3955645

it is possible to detect that Caps Lock is on, but not to set it.

If I understand right, you want to get the numbers instead of the characters on an AZERTY keyboard when Caps Lock is not engaged ?

Here is a quick project containing a subclass of TextField that generates numbers instead of characters from an AZERTY keyboard.

https://dl.dropboxusercontent.com/u/17407375/capslock.xojo_binary_project

Since it is based on the TextField TEXT it does not distinguish between dash/minus which it makes 6, and the minus sign in the numerical pad. You would have to use Keyboard.AsyncKeydown to distinguish the keys.

As it stands, the CustomTextfield always does numeric from the top row, if you use Caps Lock or not. And it works fine with a US keyboard where numbers are available without Caps Lock.

Hello Michel,

Thank you very much for your help. Exactly what I was searching for. I was searching since last evening for a solution and I did not find any.

Your help is very much appreciated. You are a very kind and helpfull person.

Wish you a very nice day and all the best.

Chris

[quote=198579:@Chris Verberne]You are a very kind and helpfull person.

[/quote]

He is, isn’t he - always posting helpful code. Good man!

Happy to help :slight_smile: Thank you for the kind words.

This evening I tested Michael Bujardet his solution again and came to the conclusion that for some keys, the result is wrong. Please take a close look to the following dictionary which converts the characters to numbers :

dico.value("&") = “1”
dico.value("") = “2”
dico.value(chr(34)) = “3”
dico.value("’") = “4”
dico.value("(") = “5”
dico.value("-") = “6”
dico.value("") = “7”
dico.value("_") = “8”
dico.value("") = “9”
dico.value("") = “0”
dico.value(")") = “”
dico.value("=") = “+”

This is a perfect French convertion for AZERTY. However the Belgium AZERTY is slightly different. Here are the results for a Belgian AZERTY keyboard :

= 6
! = 8

So I searched again for a way to determine the state of the Caps lock key and switched it “ON” (for AZERTY) or “OFF” (for QUERTY) when nummeric input without the use of the SHIFT key. The following routine is for use with a barcode scanner but can be used for other appliances too.

First create a new method with the name “InputNummeric” and give it one parameter “blnAZERTY” of type “Boolean”. Then copy the following code in the body of this method :

[code]// This function make sure the Caps lock key is always in the
// correct position to return a nummeric value depending
// on the type of keyboard. Works only in Windows.
// blnIsAZERTY values :
// blnIsAZERTY = False : keyboard is QUERTY
// blnIsAZERTY = True : keyboard is AZERTY
// blnStateCapsLock values :
// blnStateCapsLock = False : Caps lock key OFF
// blnStateCapsLock = True : Caps lock key ON
//
// When blnIsAZERTY equals “True” then the Caps lock key
// should be “ON” when producing nummeric input without
// pressing the “shift” key.
//
// Credits :
// Jim Cramer : for detecting and switching the state
// the state of the caps lock key
// Michael Bujardet : sharing his code with me and putting
// me on the right track after being lost.
// Thank you both very much for your sharing your knowledge with me.

#if TargetWin32
declare Function GetKeyState lib “User32” (key as Integer) as Integer
Declare Sub keybd_event Lib “User32” (ByVal vk as Byte, ByVal sc as Byte, _
ByVal flags as Integer, extinfo as Integer)
Select Case blnIsAZERTY
Case True // Keyboard is AZERTY
// Caps lock has to be “ON” to type numbers without shift
If Not (GetKeyState(&h14) > 0) then
// Caps lock is OFF,
// switch it on
keybd_event(&h14, &h14, 1,0)
keybd_event(&h14, &h14, 3,0)
End If
Case False // Keyboard is QUERTY
// Caps lock has to be “OFF” to type numbers
If GetKeyState(&h14) > 0 Then
// Caps lock is ON,
// switch it off
keybd_event(&h14, &h14, 1,0)
keybd_event(&h14, &h14, 3,0)
End If
End Select
#Endif[/code]

Now wherever you want to check and if necessary alter the state of the caps lock key for nummeric input, type the following code inside the “TextChange” event handler of a TextField object :

When they keyboard is AZERTY :
InputNummeric(True)

When the keyboard is QUERTY :
InputNummeric(False)

You also have to type the same calls in the “GotFocus” event handler of the textfield.

Now you can test it if you have a barcode scanner. If you do not have a barcode scanner, just enter a number with the keys on the alphanummeric part of the keyboard.

There is only one problem in one particular situation :

When the Caps lock is in state “OFF”, the text cursor is in the textfield and you call InputNummeric inside the “KeyDown” event handler of the textfield, the first character is wrong. For example, the first number of the barcode is “1”, then you will still get “&” instead. Anybody having a solution for that problem?

I like to share this method because I received so much support here within this community.

Chris

Congratulation for your work, and for sharing it.

[quote=199455:@Chris Verberne]dico.value("&") = “1”
dico.value(“é”) = “2”
dico.value(chr(34)) = “3”
dico.value("’") = “4”
dico.value("(") = “5”
dico.value("-") = “6”
dico.value(“è”) = “7”
dico.value("_") = “8”
dico.value(“ç”) = “9”
dico.value(“à”) = “0”
dico.value(")") = “°”
dico.value("=") = “+”

This is a perfect French convertion for AZERTY. However the Belgium AZERTY is slightly different. Here are the results for a Belgian AZERTY keyboard :

§ = 6
! = 8[/quote]

The difference between the two Azerty keyboards is not much of an issue. Since “!” and “§” are not part of the French Azerty, you can simply add a couple keys and both French and Belgian keyboard will work fine :

dico.value("§") = "6" dico.value("!") = "8"

Note that my solution will work (with the addition of these two keys) completely transparently between US and Azerty keyboards.

In your method, you need blnIsAZERTY . I am curious. How do you get that value ?