如何按下最后一个键? [英] How to get the last key pressed?

查看:81
本文介绍了如何按下最后一个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这就是我写的:


if(kbhit()){

a = [某些函数返回最后一个键压制];

doaction(a);

}


doaction()是我写的一个函数,但没关系。重要的是如何从键盘获取角色而不中断执行就像getch()一样。我正在考虑制作游戏,所以我需要游戏不要等待玩家按键,游戏应该继续。通过使用kbhit(),我知道是否按下了一个键,但我知道哪个键是。这是我目前面临的问题。如果有什么我没有写清楚的方式,请告诉我。

Hi guys, this is what i wrote:


if(kbhit()){
a=[some function returning last key pressed];
doaction(a);
}

doaction() is a function i wrote but it doesnt matter. What does matter is how to get a character from keyboard without interrupting execution just like getch() does. I am thinking of making a game so i need the game not to wait for the player to press a key, the game should go on. By using kbhit() i know whether a key was pressed, but i dunno which key. That is the problem i am currently facing. If there is something i didn''t write in a clear way, please let me know it.

推荐答案

我会通过在任何系统中注册信号处理程序来做到这一点你的使用。我不知道在Windows中调用它是什么(是的,我假设您使用的是Windows),但总有一些方法可以说每当事件X发生时,调用函数Y - 只要按下按键就可以请求调用你的函数,这样就可以不用轮询来查找压力,你的应用程序会在进入时通知你。
I would do this by registering a signal handler with whatever system your using. I don''t know what its called in Windows (yes, I assume your using windows) but there is always some way you can say "whenever event X occurs, call function Y" - just request to have your function called whenever a key is pressed, that way instead of polling all the time looking for presses your app gets notified of when one comes in.


哦,我只是看看kbhit()做了什么......看起来你可以像这样使用getch()

Oh, I just looked at what kbhit() does... It looks like you might be able to use getch() like this

展开 | 选择 | 换行 | 行号



我会通过使用您使用的任何系统注册信号处理程序来完成此操作。我不知道在Windows中调用它是什么(是的,我假设您使用的是Windows),但总有一些方法可以说每当事件X发生时,调用函数Y - 只需按下按键就可以调用你的函数,这样就可以不用轮询来查找压力,当你的应用程序进入时,你的应用会得到通知。
I would do this by registering a signal handler with whatever system your using. I don''t know what its called in Windows (yes, I assume your using windows) but there is always some way you can say "whenever event X occurs, call function Y" - just request to have your function called whenever a key is pressed, that way instead of polling all the time looking for presses your app gets notified of when one comes in.



但我的意思是,我怎么能用c ++做到这一点,我能想到的一种方式是while

但是我仍然无法按下最后一个键

But i mean, how can i do that in c++, the one way i can think of is by "while"
but i still can''t get the last key pressed


这篇关于如何按下最后一个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆