c ++ - GetAsyncKeyState使我的程序在打开时崩溃

时间:2017-08-12 17:13:40

标签: c++ crash

所以我正在制作一个在控制台上运行的游戏。我开始使用一些函数来使绘图更容易,并且工作正常。然后,当我尝试添加输入(使用函数GetAsyncKeyState)时,程序一启动程序就崩溃了。它说:“Text Game.exe已停止工作”这是我处理代码的方式:

Decryption Instructions:
1. A static, secret key will be shared which will be used for decryption (Secret Key TBD).
    a. HASH the secret key with SHA256, encode it to Hex and use the first 32 characters. This will be used as the KEY when decrypting.
2. Two pieces of information will be sent via the POST method
    a. Parameter “AN”: A Base64 Encoded, AES-256-CBC Encrypted string which will represent the Account Number when decrypted
    b. Parameter “IV”: A Base64 Encoded initialization vector (IV) string which will be used in decrypting the Account Number string
3. Base64 Decode both parameters
4. Using the AES-256-CBC method, decrypt the encrypted string (which was base64 decoded as part of Step #3) with the initialization vector decoded in Step #3 and the hash created in Step #1a
5. The decryption should then provide you the account number.

如果有帮助,我通过阅读这个获得了这个方法: How to check if a Key is pressed

编辑:所以我在调试模式下运行它,当我运行一个名为“refreshScreen();”的函数时,它说它崩溃了。我不知道为什么。这是代码:

if(GetAsyncKeyState('A' && 0x8000)) {
    x -= 1;
}
if(GetAsyncKeyState('D' && 0x8000)) {
    x += 1;
}
if(GetAsyncKeyState('W' && 0x8000)) {
    y += 1;
}
if(GetAsyncKeyState('S' && 0x8000)) {
    y += 1;
}

这意味着清除控制台,然后打印数组“屏幕”的所有内容。顺便说一句,“屏幕”是我写的缓冲区。

1 个答案:

答案 0 :(得分:2)

如果您尝试按照您提供的链接在答案中描述GetAsyncKeyState,那么您做错了。

Documentation说以下内容:

  

如果函数成功,则返回值指定自上次调用GetAsyncKeyState以来是否按下了键,以及该键当前是向上还是向下。如果设置了最高有效位,则键为关闭,如果设置了最低有效位,则在上一次调用GetAsyncKeyState之后按下该键。

那么你提供的链接在答案中做了什么:

if (GetAsyncKeyState('W') & 0x8000)
{ /*key is down*/ }

在if语句中按位&#34;和&#34;对GetAsyncKeyState函数和0x8000常量的返回值执行操作 - 等于0x8000最高有效位设置或等于0未设置时。< / p>

你的代码在做什么:

if(GetAsyncKeyState('A' && 0x8000)) // ...

逻辑&#34;和&#34; 'A'0x8000常量之间的操作 - 给出true,它被转换为1并作为参数传递给GetAsyncKeyState

[编辑]:正如评论中提到的,1 corresponds左键。因此,如果鼠标左键关闭,所有if条件将为true,否则为false。在xy值意外更改后,可能会在程序的不同部分出现崩溃。您应该调试程序以本地化崩溃。