我怎样才能得到在代码块中按下了什么键?
How i can get what key was pressed in codeblocks?
如何显示按下了什么键?
我的意思是,如果你按 A,屏幕上会显示:你按了 A。
cin>>keypress;
cout<<"You pressed:"<<keypress;
我想直接显示我按的是什么键。无需等待按回车并完成执行。
我想你想显示你按下的字符(就像你的例子)。所以,这很简单。这是代码:
#include <stdio.h>
#include <conio.h>
int main()
{
char keyPress;
while(1)
{
keyPress=_getch();
if((keyPress==27)||(keyPress==32))
{
printf("You decided to stop the execution of this code.");
return 0;
}
printf("You pressed:%c\n",keyPress);
}
}
如果让代码保持原样,程序将在按下 esc 或 space 时完成执行。如果你想改变这个,你可以用你的按钮的另一个ascii代码替换if((keyPress==27)||(keyPress==32))
中的数字。这是所有的 ascii 代码: https://ascii.cl/ 。如果只想一键结束程序,只需将 if((keyPress==27)||(keyPress==32))
修改为 if(keyPress==27)
即可,此时程序只会按 ESC 键停止。
我有一个 Windows 唯一的解决方案,使用 windows api。
#include <wInDoWs.h>
.
您可以使用 GetAsyncKeyState()
并将密钥的 ASCII 值传递给它。它将 return 一个简短的指示按钮的状态。据我所知,当按下按钮时,值 -32767 被 returned。将其包装在一个函数中,您可以判断按钮是否被按下。 (下面将 运行 和 copy/paste。)
#include <windows.h>
#include <iostream>
bool pressed(const short& _key)
{
short state = 0;
short pressed= -32767;
state = GetAsyncKeyState( _key);
return ( state == pressed );
}
int main()
{
//see if J is pressed
while(1)
{
if(pressed( 0x4a ) )// 'J'
std::cout << "J";
}
}
为了使它适用于所有字符,恐怕我想不出比存储所有 ASCII 值更简单的方法,以及在按下该键时要打印的内容,在一个容器中,然后每帧检查他们的按下状态。(以下只是伪代码。)
//the container this short is the 'key'
std::vector< std::pair< short , std::string > > chars;
//to check the status
for(auto& c : chars)
if( pressed( c.first ) ) std::cout << c.second;
我会把它放在某种循环中。
通过这种方式添加 'you pressed space' 并不困难。
只是做
chars.push_back( std::pair<int,std::string>(0x20 , "Spacebar") );
如何显示按下了什么键?
我的意思是,如果你按 A,屏幕上会显示:你按了 A。
cin>>keypress;
cout<<"You pressed:"<<keypress;
我想直接显示我按的是什么键。无需等待按回车并完成执行。
我想你想显示你按下的字符(就像你的例子)。所以,这很简单。这是代码:
#include <stdio.h>
#include <conio.h>
int main()
{
char keyPress;
while(1)
{
keyPress=_getch();
if((keyPress==27)||(keyPress==32))
{
printf("You decided to stop the execution of this code.");
return 0;
}
printf("You pressed:%c\n",keyPress);
}
}
如果让代码保持原样,程序将在按下 esc 或 space 时完成执行。如果你想改变这个,你可以用你的按钮的另一个ascii代码替换if((keyPress==27)||(keyPress==32))
中的数字。这是所有的 ascii 代码: https://ascii.cl/ 。如果只想一键结束程序,只需将 if((keyPress==27)||(keyPress==32))
修改为 if(keyPress==27)
即可,此时程序只会按 ESC 键停止。
我有一个 Windows 唯一的解决方案,使用 windows api。
#include <wInDoWs.h>
.
您可以使用 GetAsyncKeyState()
并将密钥的 ASCII 值传递给它。它将 return 一个简短的指示按钮的状态。据我所知,当按下按钮时,值 -32767 被 returned。将其包装在一个函数中,您可以判断按钮是否被按下。 (下面将 运行 和 copy/paste。)
#include <windows.h>
#include <iostream>
bool pressed(const short& _key)
{
short state = 0;
short pressed= -32767;
state = GetAsyncKeyState( _key);
return ( state == pressed );
}
int main()
{
//see if J is pressed
while(1)
{
if(pressed( 0x4a ) )// 'J'
std::cout << "J";
}
}
为了使它适用于所有字符,恐怕我想不出比存储所有 ASCII 值更简单的方法,以及在按下该键时要打印的内容,在一个容器中,然后每帧检查他们的按下状态。(以下只是伪代码。)
//the container this short is the 'key'
std::vector< std::pair< short , std::string > > chars;
//to check the status
for(auto& c : chars)
if( pressed( c.first ) ) std::cout << c.second;
我会把它放在某种循环中。
通过这种方式添加 'you pressed space' 并不困难。 只是做
chars.push_back( std::pair<int,std::string>(0x20 , "Spacebar") );