2

Well, you might be wondering why the use of having caps-lock always enabled no matter what. Well, for starters, I have a barcode scanning system that scans alphanumeric barcodes which includes small case and upper case letters, etc etc. When the barcode scans, the input captures the characters and verifies the barcode and does other stuff. This has caused me quite a few problems as some users have caps-lock on for x,y reason and of course, when they scan items, all the characters become capitalized and nothing works from there.

My question, is there a way to be able to "disable" (might be the wrong choice of words) the caps lock or perhaps turn it off if it's on. Essentially it would be ideal if it's detected as soon as there's an TextChanged event happening to make sure it's always off.

    private void barcodeVal_TextChanged(object sender, EventArgs e)
    {
         //check if caps lock is on. if it is, turn off and evaluate barcode
    }

Is where the it would need to happen.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dimitri
  • 1,906
  • 3
  • 25
  • 49
  • 2
    How about you leave the users alone, and simply have your software either upper case or lower case the input? Don't try to dictate to users how they use your software. Instead, you software should accommodate how the users want to use their computers. – John Saunders Aug 18 '14 at 19:47
  • @JohnSaunders the codes are case-sensitive. If I upper case the input, it's no longer the same code. – Dimitri Aug 18 '14 at 19:48
  • Then tell the users they made a mistake and say, "maybe you left the caps lock key on". – John Saunders Aug 18 '14 at 19:49
  • Here's the problem with this and it something I should have mentioned. The computer's keyboard is only accessed by managers for daily routine checks. Once they're done, they close the cabinet and lock it. Some of these managers leave caps lock on and when they launch the application for users to start scanning, things don't work anymore of course. Showing them a message won't do anything because they no longer have access to a keyboard. – Dimitri Aug 18 '14 at 19:54

2 Answers2

5

You can check it using the Control.IsKeyLocked method.

It'll end up looking like this:

if (Control.IsKeyLocked(Keys.CapsLock)) {
    // Caps lock key is on
}
else {
    // The Caps Lock key is OFF
}

I'm not positive that I would recommend it, but here's an article that describes how to turn off the Caps Lock key. Seems like you may be looking for that as well.

Community
  • 1
  • 1
Matt
  • 437
  • 3
  • 10
  • 1
    That example from MSDN is terrible. The method returns a boolean, not throws an exception! – Bryan Boettcher Aug 18 '14 at 19:50
  • @insta - You're right - changed my answer. I have no idea how that article ends up on MSDN – Matt Aug 18 '14 at 19:52
  • @Matt this simply checks if it is on caps. I need to know if it's possible to prevent caps from changing the case of the letters. Users don't have access to a keyboard to turn caps lock off. – Dimitri Aug 18 '14 at 20:12
  • @dimitri- Check out the article that I've linked to. If you need to disable caps lock, then just do that from the get-go (prior to the barcode scanning). – Matt Aug 18 '14 at 20:13
1

Upon searching, this code taken from this page page seems to work:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class CapsLockControl
{
    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo);
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;

public static void Main()
{
    if (Control.IsKeyLocked(Keys.CapsLock))
        {
            Console.WriteLine("Caps Lock key is ON.  We'll turn it off");
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
            (UIntPtr) 0);
        }
    else
        {
            Console.WriteLine("Caps Lock key is OFF");
        }
    }
}

Would it be possible for you to simply always force barcode strings into upper or lower and compare them against a similarly upper or lower cased string from your database?

HDL_CinC_Dragon
  • 194
  • 1
  • 8
  • This is a solution and it works. Unfortunately, the way the barcodes are encoded, they're case-sensitive and must be shown exactly the way they are. I'm simply trying to temporary fix this issue (the app was not written by me) until we do a full systems upgrade and software upgrade. – Dimitri Aug 18 '14 at 20:23