One of my projects I am developing is a Keyboard hook that traps some of the higher numbered function buttons (f13-f20). The tablet this will work on has buttons on it that are mapped to those higher function buttons. So any who I am making a class that has as its constructor input a Key (from System.Windows.Forms.Keys) and a AbstractTask. Since the use hook will perform various tasks I decided this would be a slick way of doing. One of the Tasks is a KeyboardTask. super simple class (I hope atleast)
public class KeyboardTask : AbstractTask
{
private KeyboardTask ()
{ }
public KeyboardTask (KeyboardCommand key)
{
Options = "{" + key + "}";
}
public override void PerformTask()
{
Globals.WriteLog("KeyboardTask:PerformTask()+");
try
{
System.Windows.Forms.SendKeys.Send(Options);
}
catch (System.Exception ex)
{
Globals.WriteExceptionLog(ex);
}
Globals.WriteLog("KeyboardTask:PerformTask()-");
}
}
public enum KeyboardCommand
{
BACKSPACE,//{BACKSPACE}, {BS}, or {BKSP}
BREAK,//{BREAK}
CAPSLOCK,//{CAPSLOCK}
DELETE,//{DELETE} or {DEL}
DOWN, //{DOWN}
END,//{END}
ENTER,//{ENTER}or ~
ESC,//{ESC}
//etc
}
So as a non-unit-test I compiled my program with this added to it
ksel1 = new KeyboardSystemEventListener((Keys.F13), new KeyboardTask(KeyboardCommand.F1));//F1
ksel6 = new KeyboardSystemEventListener((Keys.F18), new ECTask(EmbeddedControllerCommand.DecreaseBackLight));//RB
(there is a HUGE bug doing it this way, but that is already solved i just have to implement it) I put the program on my tablet, and I had IE open, pressed the F13 button and it opened IE's Help (YEAH!).. i pressed the F18 and the backlight decreased.. (no big suprise there)
so then I got to thinking.. there has to be a better way of doing (what I should have said is, why didn't I write my unit test first) So i started to write my unit test.. Problem is I don't have a F13 key.. ok not a huge deal i'll just change it to say my Home button on my keyboard, but then I tried to check for F1 and realized I have no clue how to do that with a Unit test. What i would prefer to see is something like this instead (take me out of the equation)
[Test]
public void TestKeyboardTask()
{
KeyboardTask kkt = new KeyboardTask(KeyboardCommand.F1);
kkt.PerformTask();
Assert.IsTrue(/*F1 key was pressed*/false);
}
any ideas? I'm using NUnit 2.6.2 and Visual studio 2012 pro. I prefer to use NUnit as the VS test suite doesn't seem as refined (althoough would be a billion times more conveniant if it worked)
Test Class
[Test]
public void TestKeyboardTask()
{
KeyboardTask kkt = new KeyboardTask(KeyboardCommand.F1);
using (MockKeyboardTest f = new MockKeyboardTest())
{
f.ShowDialog(kkt);
Assert.AreEqual(Keys.F1, f.PressedKey);
}
}
Mock keyboard Test
class MockKeyboardTest : Form
{
public MockKeyboardTest()
{
InitializeComponent();
PressedKey = Keys.BrowserBack;
}
public void ShowDialog(KeyboardTask kkt)
{
Keyboard = kkt;
base.ShowDialog();
}
public void InitializeComponent()
{
this.Shown += MockKeyboardTest_Shown;
KeyboardTestTextbox.AcceptsTab = true;
KeyboardTestTextbox.Location = new Point(2, 22);
KeyboardTestTextbox.MaxLength = 50;
KeyboardTestTextbox.Multiline = true;
KeyboardTestTextbox.Size = new Size(195, 25);
KeyboardTestTextbox.KeyDown += this.KeyboardTestTextbox_KeyDown;
Controls.Add(KeyboardTestTextbox);
}
void MockKeyboardTest_Shown(object sender, System.EventArgs e)
{
Keyboard.PerformTask();
}
private void KeyboardTestTextbox_KeyDown(object sender, KeyEventArgs e)
{
PressedKey = e.KeyData;
this.DialogResult = DialogResult.OK;
}
private TextBox KeyboardTestTextbox = new TextBox();
private KeyboardTask Keyboard;
public Keys PressedKey;
}
works like a charm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.