Recording keyboard key strokes
Posted on 2011-03-21
I'm a bit stuck here so I would really appreciate some help.
I'm working on a macro program that will forward certain keyboard key strokes to other keys. So let's say if I press "T" I want "U" to be returned instead.
My GUI has a text box that users can click in and specify which button will that "T" key press be forwarded to. Key press event :
private void txtTrigger_KeyPress(object sender, KeyPressEventArgs e)
txtTrigger.Text = e.KeyChar.ToString();
Then I save that Textbox's INT ascii value to the XML file as  and next time a program user hits "T" - 112 gets converted back to a string and sent back:
ConfigFile.Trigger = Char.ConvertToUtf32(txtTrigger.Text,0);
Awesome, works great with LETTERS ONLY.
I can't figuire out how to record and return other keys such as F1-12, CTRL, ALT, TAB, Backpsace...etc. They don't seem to trigger TextBox KeyPress events at all. Do I really have to go though every single Function and other keys on my keyboard wtih if (e.KeyCode == Keys.F9) individually ?
I tried using the KeyDown instead of KeyPress event and Function key presses do get picked up but KeyValues returned back to a user are incorrect:
int pressedKey = e.KeyValue;
txtTrigger.Text = char.ConvertFromUtf32(pressedKey);
I press F8 and W appears in that txtTrigger. F5 shows up as letter T.
Shift, Control and some other keys appear as .
Any help ? All I want to do is setup something like you might have seen in computer games when you setup your input bindings where you specify what key your action is forwared to.
Thank you in advance.