js enter key_如何识别用C编程语言按下的ENTER KEY?

js enter key

Here, we will learn how to get and identify that ENTER KEY is pressed?

在这里,我们将学习如何获取并确定按下了ENTER KEY ?

We will read character by character in an infinite loop and print the input characters until ENTER KEY is not pressed.

我们将无限循环地逐字符读取并打印输入的字符,直到不按ENTER KEY为止。

Within the infinite loop there will be a condition to check whether input character is ENTER KEY or not, if ENTER KEY is pressed, loop will be terminated otherwise input character will be printed and ask for Input any character again?

在无限循环内,将有条件检查输入字符是否为ENTER KEY ,如果按ENTER KEY,则循环将终止,否则将打印输入字符并要求再次输入任何字符?

检查输入键的条件 (Condition to check ENTER KEY)

There are basically two methods to check input key is ENTER KEY of not

基本上有两种方法可以检查输入键是否为ENTER KEY

  • By checking ASCII Code

    通过检查ASCII码

  • By checking new line character '\n'

    通过检查换行符'\ n'

通过检查ASCII码 (By checking ASCII Code)

The ASCII Code of ENTER KEY is 10 in Decimal or 0x0A in Hexadecimal.

ENTER KEY的ASCII码为十进制的10或十六进制的0x0A 。

Let suppose input character is storing in variable ch, then condition will be as below

假设输入字符存储在变量ch中 ,则条件如下

    if(ch==0x0A)
    {
	    //your code....
    }

    if(ch==10)
    {
	    //your code....
    }

通过检查换行符'\ n' (By checking New line character '\n')

We can also check that Input character is new line or not by comparing input character is equal to '\n' or not.

我们还可以通过比较输入字符是否等于'\ n'来检查输入字符是否为新行。

    if(ch=='\n')
    {
	    //your code....
    }


Let's consider the following program

让我们考虑以下程序

#include <stdio.h>

int main()
{
	char ch;
	//infinite loop
	while(1)
	{
		printf("Enter any character: ");
		//read a single character
		ch=fgetc(stdin);
		
		if(ch==0x0A)
		{
			printf("ENTER KEY is pressed.\n");
			break;
		}
		else
		{
			printf("%c is pressed.\n",ch);
		}
		//read dummy character to clear
		//input buffer, which inserts after character input
		ch=getchar();
	}
	return 0;
}

Output

输出量

Enter any character: A
A is pressed. 
Enter any character: B
B is pressed. 
Enter any character: 9
9 is pressed. 
Enter any character:
ENTER KEY is pressed.


翻译自: https://www.includehelp.com/code-snippets/how-to-identify-enter-key-is-pressed-in-c-programming-language.aspx

js enter key

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值