开发者

How can I change the entered key through keyboard outside my application in C#?

I am trying to change the entered key by keyboard.

I have managed to restrict key entered by keyboard.

Code in C#:

    using System.Runtime.InteropServices;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, LoadLibrary("User32"), 0);
            HookedKeys.Add(Keys.A);
            HookedKeys.Add(Keys.B);
        }

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

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

        const int WH_KEYBOARD_LL = 13;
        const int WM_KEYDOWN = 0x100;

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

        public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);
        public struct keyboardHookStruct
        {
            public int vkCode;
        }

        public int hookProc(int code, int wParam, ref keyboardHookStruct lParam)
        {
            Keys key = (Keys)lParam.vkCode;
            if (HookedKeys.Contains(key))
                return 1;
            else
                return 0;
        }

    }

After debugging check on Notepad or anywhere else in window.... A and B key will not work

How can I modify the keys, in place 开发者_开发技巧of 'A' some other key?


Since the struct is passed by ref you can modify its contents and it'll work.

And your code doesn't deal with syskeys either. And when you modify the key, then you need to take care because some applications use the virtual key code, and others the scancode to determine which key it is. So you should modify both.

But your code is broken because you don't call the next hook(using CallNextHookEx). So you're disabling all hooks except yours, which is bad.

My pascal code for this(It was a quick hack, so it's a bit ugly):

function LowLevelKeyBoardProc(nCode:   Integer;   awParam:   WPARAM;   alParam:   LPARAM):   LRESULT;   stdcall;
var
  fEatKeyStroke:   Boolean;
  replace:bool;
  replacement:TReplacement;
  p:   PKBDLLHOOKSTRUCT;
  I: Integer;
  flags:Cardinal;
  fProcessID:Cardinal;
  active:boolean;
begin
    fEatKeystroke   :=   False;
    flags:=0;
    replace:=false;
    if active and (  nCode   =   HC_ACTION)   then
    begin
        p   :=   PKBDLLHOOKSTRUCT(alParam);
        case   awParam   of
            WM_KEYDOWN,
            WM_SYSKEYDOWN,
            WM_KEYUP,
            WM_SYSKEYUP:
                begin
                  for I := 0 to length(Replacements) - 1 do
                    if Replacements[i].oldKey=p^.vkCode
                      then begin
                        replacement:=Replacements[i];
                        replace:=true;
                      end;
                end;
        end;
    end;
    if replace
      then begin
        if (awParam=WM_KEYUP)or(awParam=WM_SYSKEYUP)then
          flags:=flags or KEYEVENTF_KEYUP;
        keybd_event( p^.vkCode, replacement.newScan, flags ,0 );
        //keybd_event( replacement.newKey, replacement.newScan, flags,0 );
        fEatKeystroke:=true;
      end;
    if   fEatKeyStroke   then
        Result   :=   1
    else
        Result   :=   CallNextHookEx(hhkLowLevelKybd,   nCode,   awParam,   alParam);
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜