[Add to favorites] winform (C #) keyboard Test

Source: Internet
Author: User

Using system;
Using system. drawing;
Using system. Windows. forms;
Public class keyexamine: Form
{
Public static void main ()
{
Application. Run (New keyexamine ());
}
 
Enum eventtype
{
None, keydown, keyup, keypress
}
Struct keyevent
{
Public eventtype evttype;
Public eventargs evtargs;
}
Const int inumlines = 25;
Int inumvalid = 0;
Int iinsertindex = 0;
Keyevent [] akeyevt = new keyevent [inumlines];
Int xevent, xchar, xcode, xmod, xdata,
Xshift, xctrl, xalt, xright;
Public keyexamine ()
{
TEXT = "Key examine ";
Backcolor = systemcolors. window;
Forecolor = systemcolors. windowtext;
Xevent = 0;
Xchar = xevent + 5 * font. height;
Xcode = xchar + 5 * font. height;
Xmod = xcode + 8 * font. height;
Xdata = xmod + 8 * font. height;
Xshift = xdata + 5 * font. height;
Xctrl = xshift + 5 * font. height;
Xalt = xctrl + 5 * font. height;
Xright = xalt + 5 * font. height;
Clientsize = new size (xright, Font. Height * (inumlines + 1 ));
Formborderstyle = formborderstyle. fixed3d;
Maximizebox = false;
}
Protected override void onkeydown (keyeventargs KEA)
{
Akeyevt [iinsertindex]. evttype = eventtype. keydown;
Akeyevt [iinsertindex]. evtargs = KEA;
Onkey ();
}
Protected override void onkeyup (keyeventargs KEA)
{
Akeyevt [iinsertindex]. evttype = eventtype. keyup;
Akeyevt [iinsertindex]. evtargs = KEA;
Onkey ();
}
Protected override void onkeypress (keypresseventargs kpea)
{
Akeyevt [iinsertindex]. evttype = eventtype. keypress;
Akeyevt [iinsertindex]. evtargs = kpea;
Onkey ();
}
Void onkey ()
{
If (inumvalid <inumlines)
{
Graphics grfx = creategraphics ();
Displaykeyinfo (grfx, iinsertindex, iinsertindex );
Grfx. Dispose ();
}
Else
{
Scrolllines ();
}
Iinsertindex = (iinsertindex + 1) % inumlines;
Inumvalid = math. Min (inumvalid + 1, inumlines );
}
Protected virtual void scrolllines ()
{
Rectangle rect = new rectangle (0, fontheight,
Clientsize. Width,
Clientsize. Height-font. Height );
Invalidate (rect );
}
Protected override void onpaint (painteventargs PEA)
{
Graphics grfx = pea. graphics;
Boldunderline (grfx, "Event", xevent, 0 );
Boldunderline (grfx, "keychar", xchar, 0 );
Boldunderline (grfx, "keycode", xcode, 0 );
Boldunderline (grfx, "modifiers", xmod, 0 );
Boldunderline (grfx, "keydata", xdata, 0 );
Boldunderline (grfx, "shift", xshift, 0 );
Boldunderline (grfx, "control", xctrl, 0 );
Boldunderline (grfx, "Alt", xalt, 0 );

If (inumvalid <inumlines)
{
For (INT I = 0; I <inumvalid; I ++)
Displaykeyinfo (grfx, I, I );
}
Else
{
For (INT I = 0; I <inumlines; I ++)
Displaykeyinfo (grfx, I, (iinsertindex + I) % inumlines );
}
}
Void boldunderline (Graphics grfx, string STR, int X, int y)
{
Brush = new solidbrush (forecolor );
Grfx. drawstring (STR, Font, brush, Y, y );
Grfx. drawstring (STR, Font, brush, x + 1, y );
Sizef = grfx. measurestring (STR, font );
Grfx. drawline (new pen (forecolor), X, Y + sizef. height,
X + sizef. Width, Y + sizef. Height );
}
Void displaykeyinfo (Graphics grfx, int y, int I)
{
Brush BR = new solidbrush (forecolor );
Y = (1 + y) * font. height;
Grfx. drawstring (akeyevt [I]. evttype. tostring (), Font, BR, xevent, y );
If (akeyevt [I]. evttype = eventtype. keypress)
{
Keypresseventargs kpea =
(Keypresseventargs) akeyevt [I]. evtargs;
String STR = string. Format ("/x202d {0} (0x {1: X4 })",
Kpea. keychar, (INT) kpea. keychar );
Grfx. drawstring (STR, Font, BR, xchar, y );
}
Else
{
Keyeventargs KEA = (keyeventargs) akeyevt [I]. evtargs;
String STR = string. Format ("{0} ({1})", KEA. keycode, (INT) kea. keycode );
Grfx. drawstring (STR, Font, BR, xcode, y );
Grfx. drawstring (KEA. modifiers. tostring (), Font, BR, xmod, y );
Grfx. drawstring (KEA. keydata. tostring (), Font, BR, xdata, y );
Grfx. drawstring (KEA. Shift. tostring (), Font, BR, xshift, y );
Grfx. drawstring (KEA. Control. tostring (), Font, BR, xctrl, y );
Grfx. drawstring (KEA. alt. tostring (), Font, BR, xalt, y );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.