gpt4 book ai didi

C# GetAsyncKeyState 无法正常工作

转载 作者:行者123 更新时间:2023-12-04 03:07:45 28 4
gpt4 key购买 nike

我刚开始玩游戏模组,但遇到了瓶颈。我实际上是一名 C++ 程序员,但我目前所做的 retrofit 要求我使用 C#,这应该不是什么大问题,但我相当确定我错过了一些关键的 C# 概念。

我正在尝试将“删除”按钮绑定(bind)到使用“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# 不太熟悉,因此我将包括我尝试修改的整个类,以防问题出在其他地方。我在这里触及的唯一代码是包含更多“使用” header 并添加 if 语句和函数中的变量。(请忽略有关 token 的注释部分,它们是由反编译器引起的。)

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;
}

如果这根本不可能,是否有任何其他方法可以实现此功能?

最佳答案

这是一个简单示例,说明如何在 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 上的评论以了解更多信息.这确保我们每次按键只执行一次,而不是每次轮询键状态时执行一次。正如其他人在评论中所说,GetASyncKeyState 有利于快速证明概念,但不适合专业用途。原因之一是因为它在所有应用程序中并不安全,其他应用程序可能会设置 GetASyncKeyState() 的结果并影响您的程序。

关于C# GetAsyncKeyState 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47574045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com