测试是否在Visual C ++中按下了键 [英] Test if a key is down in Visual C++

查看:146
本文介绍了测试是否在Visual C ++中按下了键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用的是VC ++ 6.0,我在程序中按了按键时遇到了问题.当我按某个键时,我使程序执行某项操作.例如,我的计时器中有一个下降的圆圈,如果我在圆圈得到坐标Y = 500时按"Z",则我的分数增加了100.但是,如果我在圆圈得到坐标时不按"Z",则我的分数增加了500
它必须使我的分数= 0;我提高了分数,但是如果不按"Z",我不知道如何使分数无效.
我希望有一个人可以帮助我.在此先谢谢您.按下"Z"键,然后按下该功能,我的分数增加.对我来说,最难的事情是如何检查"Z"是否未按下,我的意思是我不知道该在哪里检查"Z"是否未按下.在该函数中,我检查它是否可以工作,但是在哪里检查它是否不是.
:(我看到了一个名为GetKeyState()的函数,但我不知道是否必须使用它.我的问题听起来对您来说很愚蠢,但是我才16岁,是编程的初学者.


可能您需要GetAsyncKeyState函数[ ^ ].您可以通过以下方式使用它:

bool isZKeyDown()
{
  // test if the Most Significative Bit (MSB, i.e. bit 15) is one.
  return ((GetAsyncKeyState(''Z'') >> 15) == 1);
}




顺便说一句:您应该编辑原始帖子,而不是发布新答案.
:)


Hello, i''m using VC++ 6.0 and i have a problem with pressed keys in my program. When i press some key i make my program do something. For example i have a falling circle in a timer and if i press ''Z'' when the circle gets coordinate Y = 500 it makes my score increased by 100. But if i do not press ''Z'' when the circle gets 500
it must make my scores = 0; I made it to increase my points, but i don''t know how to make it null the scores if ''Z'' is NOT pressed.
I hope someone can help me. Thanks in advance.

解决方案

int inc = -score;
if (Z is pressed)
    inc = 100;
score += inc;


Thanks a lot about your answer but i have functions where i count if ''Z'' is pressed and when it''s pressed the function is called and my score is increased. The hard stuff to me is how to check if ''Z'' is not pressed, i mean i don''t know where i need to check if ''Z'' isn''t pressed down. In the function i check if it is and it works, but where to check if it isn''t.
:( I saw a function called GetKeyState() but i have no idea if i have to use it. My questions might sound stupid to you, but i''m just 16 years and beginner in programming.


Probably you need GetAsyncKeyState function [^]. You may use it this way:

bool isZKeyDown()
{
  // test if the Most Significative Bit (MSB, i.e. bit 15) is one.
  return ((GetAsyncKeyState(''Z'') >> 15) == 1);
}




BTW: you should edit your original post, instead of posting new answers.
:)


这篇关于测试是否在Visual C ++中按下了键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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