Click here to Skip to main content
16,015,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
        }
    }
}


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
Posted
Updated 24-Jul-16 7:38am
Comments
Patrice T 24-Jul-16 11:54am    
What is the question ?
What is the problem ?
Did you try to do it yourself ?
mediamcce 24-Jul-16 12:01pm    
sendkey.send() function using keyup event not use.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900