Unity InputSystem学习笔记(一)获取设备按键信息

键盘

每帧判断

void Update()
{
     if (Keyboard.current.spaceKey.wasPressedThisFrame)
     {
         print("空格键");
     }
     if(Keyboard.current.dKey.wasReleasedThisFrame)
     {
         print("d键抬起");
     }
     if(Keyboard.current.spaceKey.isPressed)
     {
         print("空格按下");
     }
     if(Keyboard.current.anyKey.wasPressedThisFrame)
     {
         print("任意键按下");
     }
}


绑定事件的方式

keyboard.onTextInput += (c) =>
{
    print("通过Lambda表达式" + c);
};
keyboard.onTextInput += TestInput;
private void TestInput(char c)
{
    print("通过函数监听" + c);
}

鼠标

每帧判断

void Update
{
	if(Mouse.current.rightButton.wasPressedThisFrame)
    {
        print("鼠标右键按下");
    }
    if(Mouse.current.middleButton.wasPressedThisFrame)
    {
        print("鼠标中建按下");
    }
    if(Mouse.current.forwardButton.wasPressedThisFrame)
    {
        print("鼠标前键按下");
    }
    if(Mouse.current.backButton.wasPressedThisFrame)
    {
        print("鼠标后键按下");
    }
    //屏幕坐标(左下角为(0,0)
    print(Mouse.current.position.ReadValue());
    //两帧之间的偏移
    print(Mouse.current.delta.ReadValue());
    //滚轮
    print(Mouse.current.scroll.ReadValue());
}

触摸屏

每帧判断


void Update
{
	Touchscreen ts = Touchscreen.current;
	if (ts == null)
	{
	 	return;
	}
	else
	{
		 TouchControl tc = ts.touches[0];
		 if(tc.press.wasPressedThisFrame)
		 {
		     print("按下");
		 }
		 if(tc.press.wasReleasedThisFrame)
		 {
		     print("抬起");
		 }
		 if(tc.press.isPressed)
		 {
		     print("按住");
		 }
		 if(tc.tap.isPressed)
		 {
		
		 }
		 //点击次数 
		 print(tc.tapCount);
		 //点击位置
		 print(tc.position.ReadValue());
		 //第一次接触位置
		 print(tc.startPosition.ReadValue());
		 //得到的范围
		 print(tc.radius.ReadValue());
		 //偏移位置
		 print(tc.delta.ReadValue());
		 //返回TouchPhase: None,Began,Moved,Ended,Canceled,Stationary
		 print(tc.phase.ReadValue());
	
	 //判断状态
		 UnityEngine.InputSystem.TouchPhase tp = tc.phase.ReadValue();
		 switch (tp)
		 {
		     //无
		     case UnityEngine.InputSystem.TouchPhase.None:
		         break;
		         //开始接触
		     case UnityEngine.InputSystem.TouchPhase.Began:
		         break;
		         //移动
		     case UnityEngine.InputSystem.TouchPhase.Moved:
		         break;
		         //结束
		     case UnityEngine.InputSystem.TouchPhase.Ended:
		         break;
		         //取消
		     case UnityEngine.InputSystem.TouchPhase.Canceled:
		         break;
		         //静止
		     case UnityEngine.InputSystem.TouchPhase.Stationary:
		         break;
		 }
}

手柄

Gamepad gamepad = Gamepad.current;
    if(gamepad==null)
    {
        return;
    }
    Vector2 leftDir= gamepad.leftStick.ReadValue();
    Vector2 rightDir= gamepad.rightStick.ReadValue();
    //左摇杆按下抬起
    if(Gamepad.current.leftStickButton.wasPressedThisFrame)
    {

    }
    if (Gamepad.current.leftStickButton.wasReleasedThisFrame)
    {

    }
    if (Gamepad.current.leftStickButton.isPressed)
    {

    }
    //右摇杆按下抬起
    if (Gamepad.current.rightStickButton.wasPressedThisFrame)
    {

    }
    if (Gamepad.current.rightStickButton.wasReleasedThisFrame)
    {

    }
    if (Gamepad.current.rightStickButton.isPressed)
    {

    }

    if(Gamepad.current.dpad.left.wasPressedThisFrame)
    {

    }
    if (Gamepad.current.dpad.left.wasReleasedThisFrame)
    {

    }
    if (Gamepad.current.dpad.left.isPressed)
    {

    }
    //右侧三角方块/XYAB按键
    //Gamepad.current.buttonEast;
    //Gamepad.current.buttonWest;
    //Gamepad.current.buttonSouth;
    //Gamepad.current.buttonEast;
    if (Gamepad.current.buttonNorth.wasPressedThisFrame)
    {

    }
    if (Gamepad.current.buttonNorth.wasReleasedThisFrame)
    {

    }
    if (Gamepad.current.buttonNorth.isPressed)
    {

    }
    //手柄中央键
    if(Gamepad.current.startButton.wasPressedThisFrame)
    {

    }
    if(Gamepad.current.selectButton.wasPressedThisFrame)
    {

    }
    //肩键
    if(Gamepad.current.leftShoulder.wasPressedThisFrame)
    {

    }
    if (Gamepad.current.rightShoulder.wasPressedThisFrame)
    {

    }
    if(Gamepad.current.leftTrigger.wasPressedThisFrame)
    {

    }
    if(Gamepad.current.rightTrigger.wasPressedThisFrame)
    {

    }
### 笔记本电脑键盘和触摸板无响应的解决方案 #### BIOS 设置检查 对于某些设备而言,BIOS 中可能存在禁用触摸板或特定输入设备的选项。进入 BIOS 并确认这些设置是否正确可以排除潜在问题[^1]。 #### 快捷键状态验证 许多笔记本配备有专门的功能键来切换触摸板的状态。通常会有个图标表示手指点击手势旁边带有开启/关闭标志的按钮。按此组合键可重新激活触摸板功能。 #### 驱动程序更新与重装 过时或损坏的驱动可能是造成触摸板不工作的原因之。前往制造商官网下载最新版本的触摸板驱动并安装;如果已有的驱动出现问题,则考虑卸载后再清洁安装新驱动。 #### 外部设备干扰排查 当遇到触摸板无响应的情况时,尝试断开所有外接USB 设备(如U盘、打印机等),因为它们可能会引起资源冲突从而影响内建控制器的工作效率[^5]。 #### 软件层面调试 针对Unity引擎开发的应用场景下,旧版Input System可能导致按键映射与其他应用程序发生冲突进而使得部分交互失效。此时应审查代码中的事件监听机制,并适当调整优先级以确保预期行为得以实现[^3]。 #### 键盘模块维护 如果是Stront分岛键盘这类特殊结构的产品,在面对个别按键失灵现象时,需按照官方文档指导拆解相应区域并对内部连接线路做进步检验。另外,学习基础焊接技巧有助于自行修复轻微物理损伤带来的接触不良状况[^2]。 ```c // C语言示例:检测矩阵式键盘扫描行列是否有短路情况 for(int row=0;row<ROW_COUNT;++row){ setRow(row, LOW); for(int col=0;col<COLUMN_COUNT;++col){ if(digitalRead(col)==LOW){ // 发现闭合电路 printf("Key at position (%d,%d)\n", row,col); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值