By writing a console, the output of user buttons is simple and small.ProgramUsed to practice some operations related to the input keys in the console (system. Console class.
Obtain console key information
The function that gets the key information in the console and determines whether the key character is output is console and readkey. It returns a lelekeyinfo.
Class Console
Static ConsolekeyinfoReadkey (BoolDo you want to display buttons );
The consolekeyinfo class contains detailed information about the buttons:
Class Consolekeyinfo
Key:Consolekey;
// Return consolekey enumeration, containing specific buttons, such as escape (ESC), A-Z, D1 (number 1), F1...
Modifiers:Lelemodifiers;
// Return lelemodifiers enumeration, which indicates the modifier keys in the combination key: Ctrl, ALT, and shift. If there is no combination key, the value is 0.
Keychar:Char;
// Indicates the text value that can be displayed by pressing the key. It is also the text value that can be set by console. readkey.
In addition, there are other special Buttons such as case and keypad keys. You can use the console. capslock and console. numberlock attributes to determine.
The console class also provides the keyavailable attribute, which works with the console. readkey method to show whether there is a key input.
Console Ctrl + C and cancel event
You can press Ctrl + C to terminate the current console. In the following example, only the ESC key is used to terminate user input, therefore, we need to Disable Ctrl + C to terminate console input.
The first method is to directly use the treatcontrolcasinput attribute of the console class and set it to true. In this way, CTRL + C is a normal key input without terminating the console.
Another method is to use the cancelkeypress event of the console class:
The cancelkeypress event of the console class will occur when the CTRL + C button is pressed on the console, and the cancel attribute is in lelecanceleventargs to specify whether to cancel the console operation. If it is set to true, CTRL + C will not end the console any more.
Console.Cancelkeypress+ =(Sender, E)=>
{
E.Cancel= True;
// Ctrl + C close attempt canceled,
};
Sample Applet
Code:
Public Static VoidMain ()
{
// Disable Ctrl + C to terminate the Console
Console.Treatcontrolcasinput= True;
While(True)
{
// Retrieve lelekeyinfo without displaying the Input key characters
ConsolekeyinfoKey= Console.Readkey (True);
// Exit ESC
If(Key.Key= Consolekey.Escape)
Break;
// Output key information
Addkey (Key.Key, key.Modifiers );
}
}
Static VoidAddkey (ConsolekeyKey,LelemodifiersMoD)
{
If((Int) Mod! = 0)
Console.Write (mod.Tostring ()+ "+");
Console.Writeline (key );
}