在VB.NET中使用全局keyboardhookup更改键序列 [英] Change key sequence using global keyboardhookup in VB.NET

查看:89
本文介绍了在VB.NET中使用全局keyboardhookup更改键序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

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

namespace key_preview {
    public partial class Form1 : Form {
        globalKeyboardHook gkh = new globalKeyboardHook();

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {

            gkh.HookedKeys.Add(Keys.A);
            gkh.HookedKeys.Add(Keys.B);
            gkh.HookedKeys.Add(Keys.C);
            gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
            gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
        }

        void gkh_KeyUp(object sender, KeyEventArgs e) {
            if ( e.KeyCode == Keys.A)
            {
                   SendKeys.Send("c");
             }
            if (e.KeyCode == Keys.C)
            {
                    SendKeys.Send ("b");

            }
            if (e.KeyCode == Keys.B)
            {
                    SendKeys.Send("a");
            }
            e.Handled = true;

        }

        void gkh_KeyDown(object sender, KeyEventArgs e) {

            e.Handled = true;
        }
    }
}





我尝试过:



我按键A显示b并按b显示c。我想按键A显示c并按b显示



What I have tried:

I press key A display b and press b display c. I want to press key A display c and press b display a

推荐答案

除非你想再次激活事件,否则你不能在Key事件中使用SendKeys,再次,...



在一个真正的GlobalKeyboard Hook中,如果这是你真正使用的,你必须修改钩子获得的键值,然后修改后的键值被传递到钩子链上。



你不能使用SendKeys将一个键换成另一个键,因为你没有修改键,你是发送一个新的。如果原始键未被禁止,则最终会按下两个键而不是一个。
You cannot use SendKeys inside a Key event unless you want to fire the event again, and again, and again, ...

In a real GlobalKeyboard Hook, if that's what you're really using, you have to modify the key value the hook gets and then that modified key value is passed up the hook chain.

You can NOT use SendKeys to swap out one key for another because you're not modifying the key, you're sending a new one. If the original key is not suppressed you'll end up getting two keys pressed instead of one.


这篇关于在VB.NET中使用全局keyboardhookup更改键序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆