. Net (C #): system. Console console and button operations

Source: Internet
Author: User

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 );

}

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.