如何在数字键盘上使用Unicode?

时间:2016-04-18 13:26:55

标签: c# unicode unicode-string

我希望你能帮助我,我有一个应用程序,它按下了我按下的每一个键,就像一个KeyLogger(是的,我知道,它是一种恶意软件,但我习惯了另一件事,好东西,非常好的东西。不适合间谍某人)。当我按下字母键和F键之间的数字键时(我的意思是,如果键盘上没有数字小键盘,那么你可以使用的是字母上面的键),它的写法如下:

0123456789

但是当我使用NumericPad(键盘右侧的那个)时,它显示如下:

ABCDEFGHIJ

如何将信件转换为数字?

PS:我对此进行了研究,发现我可能需要使用UnicodeEncodig.Unicode,但我不知道如何使用它。

这是捕获键的类:

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;   

public class GlobalKeyboardHook
{
    [DllImport("user32.dll")]
    static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam);

[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID);

[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);

public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam);

public struct keyBoardHookStruct
{
    public int vkCode;
    public int scanCode;
    public int flags;
    public int time;
    public int dwExtraInfo;
}

const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int WM_SYSKEYDOWN = 0x0104;
const int WM_SYSKEYUP = 0x0105;

LLKeyboardHook llkh;
public List<Keys> HookedKeys = new List<Keys>();

IntPtr Hook = IntPtr.Zero;

public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;

// This is the Constructor. This is the code that runs every time you create a new GlobalKeyboardHook object
public GlobalKeyboardHook()
{
    llkh = new LLKeyboardHook(HookProc);
    // This starts the hook. You can leave this as comment and you have to start it manually (the thing I do in the tutorial, with the button)
    // Or delete the comment mark and your hook will start automatically when your program starts (because a new GlobalKeyboardHook object is created)
    // That's why there are duplicates, because you start it twice! I'm sorry, I haven't noticed this...
    hook(); <-- Choose!
}
~GlobalKeyboardHook()
{ unhook(); }

public void hook()
{
    IntPtr hInstance = LoadLibrary("User32");
    Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0);
}

public void unhook()
{
    UnhookWindowsHookEx(Hook);
}

public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam)
{
    if (Code >= 0)
    {
        Keys key = (Keys)lParam.vkCode;
        if (HookedKeys.Contains(key))
        {
            KeyEventArgs kArg = new KeyEventArgs(key);
            if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                KeyDown(this, kArg);
            else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                KeyUp(this, kArg);
            if (kArg.Handled)
                return 1;
        }
    }
    return CallNextHookEx(Hook, Code, wParam, ref lParam);
}

}

第二次编辑,这是我使用gHook_KeyDown的地方:

GlobalKeyboardHook gHook;
private void Form1_Load(object sender, EventArgs e)
{
   gHook = new GlobalKeyboardHook();
   gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
   foreach(Keys key in Enum.GetValues(typeof(Keys)))
         ghook.HookedKeys.Add(key);
}

public void gHook_KeyDown(object sender, KeyEventArgs e)
{
    textBox1.Text += ((char)e.KeyValue).ToString();
}

0 个答案:

没有答案