当大写字母打开时,C#KeyEventArgs发出错误的输出

时间:2014-03-19 03:40:14

标签: c# keyboard keylistener keyeventargs

当窗口失焦时,我使用global keyboard hooks作为键盘侦听器。我已经完成了一些代码,我意识到无论出于何种原因,使用大写锁定会更改KeyEventArgs的输出。使用shift / no shift输出普通结果但是如果我将大写锁定,A将变为U,B将变为V,C将变为W等等。以下是执行此操作的代码(如果需要,将添加全局键盘挂钩):

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());
            }
        }
    }

我该如何解决这个问题?我有没有想法为什么它完全改变了这封信,因为我把大写锁定了。 Shift工作正常。

1 个答案:

答案 0 :(得分:1)

您链接到的globalKeyboardHook课程使用此行进行AddModifiers(Keys key)方法:

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

此代码检查Cap Lock是否打开,如果是,则检查稍后由该方法返回的key变量。

问题是Caps Lock不是修饰键,不应该以这种方式操作。这就是您在大写锁定时所看到的奇怪键值的原因。

顺便说一句,只有Control,Shift和Alt是修饰键,AddModifiers()处理这些键。

如果你删除这一行,我相信你会没事的。