using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KeyEvents02
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDownTranslator); this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress); txt.Focus();
}
// StringBuilder strbMsg = new StringBuilder();
string stringFormat = "{0,-12} = {1}\r\n";
private int m_actualKey = 0;
public int ActualKey
{
get { return m_actualKey; }
set { m_actualKey = value; }
}
private string m_theKeys = string.Empty;
public string TheKeys
{
get { return m_theKeys; }
set { m_theKeys = value; }
}
private void txt_KeyDownTranslator(object sender, KeyEventArgs e)
{
// strbMsg.Clear(); //not available in .NET 3.5, so make the strbMsg local
StringBuilder strbMsg
= new StringBuilder
(); txt.Text = "";
/*
* If NO Modifier [CTRL, ALT, SHIFT] is active accept ONLY the F1 -> F15 keys
* or the non-printable keys :
* PDUP, PGDN, END, HOME, LEFT, UP, RIGHT, DOWN, INSERT, DELETE
*
* */
KeysConverter kc
= new KeysConverter
(); int keyValue = e.KeyValue;
long modifierKey = 0;
ActualKey = 0;
TheKeys = string.Empty;
//
// if the last key was a modifier, do nothing 'cause we don't know the actual key yet.
if ((keyValue == 16) //Shift
| (keyValue == 17) // Control
| (keyValue == 18) // Alt
)
{
// MessageBox.Show(keyValue.ToString(), "MODIFIER KEY");
return;
}
//
// If a Modifier has been defined
// test if the current keyCode is suitable
else if (e.Control | e.Alt | e.Shift)
{
if (
(keyValue == 8) // Backspace
| (keyValue == 9) // Tab
| (keyValue == 13) // Enter
| (keyValue == 19) // Pause/Break
| (keyValue == 20) // CapLock
| (keyValue == 27) // ESC
| (keyValue == 144) // NumLock
| (keyValue == 145) // ScrLk
)
{
MessageBox.Show(e.Modifiers.ToString()
+ "\n"
+ kc.ConvertToString(e.KeyCode)
, "INVALID KEY");
return;
}
//
// A Modifier and actual key are valid, so assign the values.
//
else
{
modifierKey = (e.Alt ? 1 : 0)
+ (e.Control ? 2 : 0)
+ (e.Shift ? 4 : 0);
ActualKey = keyValue;
TheKeys = (e.Control ? "CTRL+" : string.Empty)
+ (e.Alt ? "ALT+" : string.Empty)
+ (e.Shift ? "SHIFT+" : string.Empty)
+ e.KeyCode;
}
}
//
// We know the keyValue is not a modifier
// and we know there has been no modifier defined, so
// Test if the keyValue is F1-> F15 or
// PDUP, PGDN, END, HOME, LEFT, UP, RIGHT, DOWN, INSERT, DELETE
//
else if (
((keyValue >= 33) & (keyValue <= 46))
| ((keyValue >= 112) & (keyValue <= 126))
)
{
//MessageBox.Show(kc.ConvertToString(e.KeyCode)
// , "UNMODIFIED KEY");
modifierKey = 0;
ActualKey = keyValue;
TheKeys = e.KeyData.ToString();
}
else
{
// no valid option, so return
return;
}
txt.Text = TheKeys;
strbMsg.AppendFormat(stringFormat, "KeyCode", kc.ConvertToString(e.KeyCode));
strbMsg.AppendFormat(stringFormat, "Converted", TheKeys);
strbMsg.AppendFormat(stringFormat, "modifierKey", modifierKey);
strbMsg.AppendFormat(stringFormat, "actualKey", ActualKey);
strbMsg.AppendLine("//--------------------");
txtOutput.AppendText(strbMsg.ToString());
}
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the KeyValue being set in the KeyDown event.
if (TheKeys == string.Empty)
{
// Stop the character from being entered into the control since it is not a suitable value.
e.Handled = true;
}
}
private void ClearInput_Click(object sender, EventArgs e)
{
txt.Clear();
txt.Focus();
}
private void ClearOutput_Click(object sender, EventArgs e)
{
txtOutput.Clear();
txt.Focus();
}
}
}