C#GetAsyncKeyState 不起作用

Posted

技术标签:

【中文标题】C#GetAsyncKeyState 不起作用【英文标题】:C# GetAsyncKeyState not functioning 【发布时间】:2018-05-14 10:00:37 【问题描述】:

我刚开始做游戏模组,但我碰壁了。 我实际上是一名 C++ 程序员,但我目前正在做的修改需要我使用 C#,这应该不是什么大问题,但我相当肯定我错过了一些关键的 C# 概念。

我正在尝试将“删除”按钮绑定到使用“GetAsyncKeyState”递增变量的函数。我已经尝试了该功能的所有变体,类型转换等,但没有任何效果。下面是整个函数,还有一些我在下面尝试过的变体示例。

功能:

private void incModJump()

   if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
   
      Thread.Sleep(150);
      this.modJump += 1f;
      this.modWallJump += 1f;
   

我尝试过的“if”语句的变体。

(这些都在使用和不使用“Convert.ToBoolean”的情况下进行了测试,都不起作用。)

if (GetAsyncKeyState(127) > 0)
if (GetAsyncKeyState(127) & 0x8000)
if (GetAsyncKeyState(127) & 0x8000 == 0x8000)
if (GetAsyncKeyState(127) & 32768)
if (GetAsyncKeyState(127) & -32768)

上述方法似乎都不起作用,我不知道如何实现这个功能,我也看过使用这个函数的示例 C# 代码,所以我怀疑这是一个语法错误(编译器也会警告我的。)

如前所述,我是一名 C++ 程序员,所以这可能是因为我对 C# 不太熟悉,因此我将包含我要修改的整个类,以防问题出在其他地方。 我在这里触及的唯一代码是包含更多“使用”标题并添加 if 语句中的变量和函数。 (请忽略关于标记的注释部分,它们是由反编译器引起的。)

using System;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;

// Token: 0x0200007B RID: 123
public class Movement : MonoBehaviour

// Token: 0x06000282 RID: 642
private void Start()

    this.fighting = base.GetComponent<Fighting>();
    this.standing = base.GetComponent<Standing>();
    this.info = base.GetComponent<CharacterInformation>();
    this.controller = base.GetComponent<Controller>();
    this.grabHandler = base.GetComponent<GrabHandler>();
    this.au = base.GetComponentInChildren<Audiosource>();
    BodyPart[] componentsInChildren = base.GetComponentsInChildren<BodyPart>();
    this.rigidbodies = new Rigidbody[componentsInChildren.Length];
    for (int i = 0; i < this.rigidbodies.Length; i++)
    
        this.rigidbodies[i] = componentsInChildren[i].GetComponent<Rigidbody>();
    
    this.screenshake = ScreenshakeHandler.Instance;
    this.rightHand = base.GetComponentInChildren<RightHand>().GetComponent<Rigidbody>();
    this.leftHand = base.GetComponentInChildren<LeftHand>().GetComponent<Rigidbody>();


// Token: 0x06000283 RID: 643
private void FixedUpdate()

    this.flyVelocity *= 0.95f;
    if (this.controller.canFly)
    
        this.MoveFly(this.flyVelocity);
        this.MoveFly(Vector3.up * 0.37f);
        this.leftHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * 2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
        this.rightHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * -2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
    


// Token: 0x06000284 RID: 644
private void MoveFly(Vector3 direction)

    if (this.info.sinceFallen < 0f)
    
        return;
    
    Rigidbody[] array = this.rigidbodies;
    for (int i = 0; i < array.Length; i++)
    
        array[i].AddForce(direction * this.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
    
    foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
    
        rigidbodyMovement.rigidbody.AddForce(direction * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
    


// Token: 0x06000285 RID: 645
public void Fly(Vector3 direction)

    this.flyVelocity += direction * Time.deltaTime * 10f;


// Token: 0x06000286 RID: 646
public void MoveRight()

    if (this.info.sinceFallen < 0f)
    
        return;
    
    float num = 1f;
    if (!this.controller.isAI)
    
        num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
    
    if (this.grabHandler.isHoldingSomething)
    
        num *= 0.1f;
    
    Rigidbody[] array = this.rigidbodies;
    for (int i = 0; i < array.Length; i++)
    
        array[i].AddForce(-Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    
    foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
    
        rigidbodyMovement.rigidbody.AddForce(-Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    


// Token: 0x06000287 RID: 647
public void Move(float direction)

    if (this.info.sinceFallen < 0f)
    
        return;
    
    float num = 1f;
    if (!this.controller.isAI)
    
        num = Mathf.Abs(this.controller.PlayerActions.Movement.X);
    
    if (this.grabHandler.isHoldingSomething)
    
        num *= 0.1f;
    
    Rigidbody[] array = this.rigidbodies;
    for (int i = 0; i < array.Length; i++)
    
        array[i].AddForce(direction * Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    
    foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
    
        rigidbodyMovement.rigidbody.AddForce(direction * Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    


// Token: 0x06000288 RID: 648
public void MoveLeft()

    if (this.info.sinceFallen < 0f)
    
        return;
    
    float num = 1f;
    if (!this.controller.isAI)
    
        num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
    
    if (this.grabHandler.isHoldingSomething)
    
        num *= 0.1f;
    
    Rigidbody[] array = this.rigidbodies;
    for (int i = 0; i < array.Length; i++)
    
        array[i].AddForce(Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    
    foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
    
        rigidbodyMovement.rigidbody.AddForce(Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
    


// Token: 0x06000289 RID: 649
public bool Jump(bool force = false, bool forceWallJump = false)

    bool result = this.DoJump(force, forceWallJump);
    this.au.PlayOneShot(this.jumpClips[UnityEngine.Random.Range(0, this.jumpClips.Length)]);
    return result;


// Token: 0x0600028A RID: 650
private bool DoJump(bool force = false, bool forceWallJump = false)

    bool result = false;
    this.standing.gravity = this.jumpTime * 0.5f;
    float d = 0.3f;
    foreach (Rigidbody rigidbody in this.rigidbodies)
    
        rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
        if (!force)
        
            if (this.info.wallNormal != Vector3.zero)
            
                rigidbody.AddForce(this.info.wallNormal * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
                rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
                result = true;
            
            else
            
                rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modJump, ForceMode.VelocityChange);
                result = false;
            
        
        else if (forceWallJump)
        
            rigidbody.AddForce(this.info.wallNormal * d * this.jumpForceMultiplier * 0.75f, ForceMode.VelocityChange);
            rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier * 0.85f, ForceMode.VelocityChange);
        
        else
        
            rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier, ForceMode.VelocityChange);
        
    
    this.screenshake.AddShake(Vector3.up * 0.01f);
    return result;


// Token: 0x060014F1 RID: 5361
private void incModJump()

    if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
    
        Thread.Sleep(150);
        this.modJump += 1f;
        this.modWallJump += 1f;
    


// Token: 0x06001526 RID: 5414
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int nVirtKey);

// Token: 0x040002CB RID: 715
public RigidbodyMovement[] rigsToMove;

// Token: 0x040002CC RID: 716
public float forceMultiplier;

// Token: 0x040002CD RID: 717
public float jumpForceMultiplier;

// Token: 0x040002CE RID: 718
public float jumpTime = 0.5f;

// Token: 0x040002CF RID: 719
private Standing standing;

// Token: 0x040002D0 RID: 720
private CharacterInformation info;

// Token: 0x040002D1 RID: 721
private Controller controller;

// Token: 0x040002D2 RID: 722
private GrabHandler grabHandler;

// Token: 0x040002D3 RID: 723
private Fighting fighting;

// Token: 0x040002D4 RID: 724
private Rigidbody[] rigidbodies;

// Token: 0x040002D5 RID: 725
private ScreenshakeHandler screenshake;

// Token: 0x040002D6 RID: 726
private AudioSource au;

// Token: 0x040002D7 RID: 727
public AudioClip[] jumpClips;

// Token: 0x040002D8 RID: 728
public Vector3 flyVelocity = Vector3.zero;

// Token: 0x040002D9 RID: 729
private Rigidbody leftHand;

// Token: 0x040002DA RID: 730
private Rigidbody rightHand;

// Token: 0x04001332 RID: 4914
private float modJump = 3f;

// Token: 0x04001333 RID: 4915
private float modWallJump = 3.75f;

如果这根本不可能,我还有其他方法可以实现此功能吗?

【问题讨论】:

为什么是127?根据this list,删除键是 0x2E 或十进制 46,而 127 或 0x7F 是 F15 键(Shift+F3?) 我会问Why GetAsyncKeyState 而不是处理键盘事件? @oerkelens 是的,这对我来说是一个奇怪的错误,但它没有任何区别。这一切最初都是用“+”测试的,但它对任何东西都不起作用 @PanagiotisKanavos 就像提到的那样,我不习惯 C#,但你能指出我正确的方向吗? 这与 Windows 编程有关,而不是 C#。即使在 C++ 中,您也会检查 Windows 消息,而不是在循环中检查密钥。 应用程序 无法控制键盘,也不知道发生了什么。操作系统向应用程序发送有关点击、按键、绘画事件、要求他们重绘自己等的消息。 【参考方案1】:

这是一个简单的例子,说明如何在 C# 中使用 GetASyncKeyState() 来检查是否按下了某个键。

[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Int32 vKey);

int VK_DELETE = 0x2E;
            
while (true)

    short keyStatus = GetAsyncKeyState(VK_DELETE);

    if ((keyStatus & 1) == 1)
    
        //if you land here, the key was pressed
    

我们正在检查是否设置了最低有效位,请在MSDN 的评论中了解更多信息。这确保我们每次按键只执行一次,而不是每次轮询键状态。正如其他人在 cmets 中所说,GetASyncKeyState 适合快速证明概念,但不适用于专业用途。原因之一是它在所有应用程序中都不是安全的,其他应用程序可能会设置 GetASyncKeyState() 的结果并影响您的程序。

【讨论】:

以上是关于C#GetAsyncKeyState 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

GetAsyncKeyState(int vKey)中的点逗号减号

通过 POST 发送 JSON 不起作用 - 目标 C

C# FindWindowEx 通过 WinSpy++ 获取 lpszClass 变量的参数不起作用

将 GetAsyncKeyState() 重置为默认值

来自windows.h的GetAsyncKeyState

为啥在 Win32 中使用 `GetAsyncKeyState()` 时我的热键会出错?