You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UE4中如何通过Blueprints或C++禁用ESC等按键功能

UE4禁用ESC或任意自定义按键的实现方案

以下分别提供蓝图和C++两种实现路径:

蓝图实现

  • 首先进入项目设置的「输入」分类,确认ESC对应的输入映射名称,UE默认通常为Cancel或Escape
  • 打开需要生效的PlayerController蓝图,或对应Pawn/Character蓝图
  • 在事件图表中添加对应输入映射的触发事件(如Input Action Cancel)
  • 该事件的执行引脚不连接任何后续逻辑即可拦截原有响应,如果此前该事件绑定了退出等逻辑,删除原有连线即可
  • 若需要全局生效,可在Game Instance中绑定该输入事件做空处理,或在PlayerController中设置输入模式为捕获所有输入

C++实现

方案1:覆盖输入事件响应

在自定义PlayerController类中绑定ESC输入事件做空实现,即可覆盖默认行为:

// 头文件声明
UFUNCTION()
void OnEscapePressed();

// 源文件中绑定事件
void AYourPlayerController::BeginPlay()
{
    Super::BeginPlay();
    InputComponent->BindAction("Escape", IE_Pressed, this, &AYourPlayerController::OnEscapePressed);
}

// 空实现即可拦截原有响应
void AYourPlayerController::OnEscapePressed()
{
    // 无逻辑
}

方案2:输入阶段直接拦截

如果需要彻底屏蔽该按键的所有分发,可重写InputKey方法:

// 头文件中重写声明
virtual bool InputKey(FKey Key, EInputEvent EventType, float AmountDepressed, bool bGamepad) override;

// 源文件实现
bool AYourPlayerController::InputKey(FKey Key, EInputEvent EventType, float AmountDepressed, bool bGamepad)
{
    // 检测到ESC按键直接返回已处理,不再向下分发
    if (Key == EKeys::Escape)
    {
        return true;
    }
    return Super::InputKey(Key, EventType, AmountDepressed, bGamepad);
}

注意:如果仅需要禁用编辑器运行时按ESC退出PIE的默认行为,直接在编辑器偏好设置的「播放」分类下,取消勾选「按Escape退出播放」即可,无需修改游戏代码。


内容的提问来源于stack exchange,提问作者Technika číslo 1 s.r.o

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 23:00:05