Download

Mapping Gamepad input to mouse events in C++

Hey,
What I want to do is have the A button on an Xbox controller trigger a left mouse click. So that pressing the A button on the gamepad has the exact same effect as clicking the left mouse button.

I was wondering if this would be possible to do through C++ in Unreal, or if there was another way.

I have the source code to a program which does this, but I’m not really a programmer and I am unsure whether it is even relevant.
http://altcontroller.net/faq.aspx

Does anyone have any ideas, or whether such a thing is possible?

All you need is to change the ini file. See here: https://wiki.unrealengine.com/List_of_Key/Gamepad_Input_Names

Hm, I had been looking for that as that is how I would have done it in UDK, hopefully it is that simple, I will see if it works. Thanks Lynx

But actually, I know I can set action mappings in the .ini so that the two buttons have the same effect, but can I make so that the A button is actually ‘creating’ a left click? (If so I can’t work out how to do that in the .ini.)
Meaning that, pressing the A button would also click buttons in in-game menus etc. as if they were left clicked.

Any luck with this Mosel3y?

Was anyone able to figure out a good way to do this using the .ini file?

I didn’t end up progressing any further on this, however 7hny responded to a AnswerHub question recently that might help you, see here How to move mouse cursor with Gamepad stick? - UE4 AnswerHub
Their response:

edit: Sorry, didn’t realise that was an old reply, I wasn’t notified

Just to update, to anyone who comes across this on Google, this works in Windows by doing the following (something I also found through Google…).

I did this in the player controller, a blueprint node to create a left mouse input:

void AMyPlayerController::BClickDown()
{
INPUT Input = { 0 };
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &Input, sizeof(INPUT));

}

void AMyPlayerController::BClickUp()
{
INPUT Input = { 0 };
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &Input, sizeof(INPUT));
}

It’s not perfect, but it works more often than not.
Two functions, one for mouse down, one for mouse up.
Using InputKey() did not work for me.

Here is a working example for a custom player controller in c++.
It injects a native windows input event, so it only works on Windows platforms.


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"

/**
 *
 */
UCLASS()
class MY_API AMyPlayerController : public APlayerController
{
    GENERATED_BODY()

    AMyPlayerController();

    UFUNCTION(BlueprintCallable, Category = "My")
    static void LeftMouseButtonDown();

    UFUNCTION(BlueprintCallable, Category = "My")
    static void LeftMouseButtonUp();
};
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyPlayerController.h"
#include "AllowWindowsPlatformTypes.h"
#include "WinUser.h"

//Constructor
AMyPlayerController::AMyPlayerController()
    : APlayerController()
{
    //... custom defaults ...
}

void AMyPlayerController::LeftMouseButtonDown()
{
    INPUT Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dx = 256; // If you need to set the position
    Input.mi.dy = 256; // If you need to set the position
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1, &Input, sizeof(INPUT));

}


void AMyPlayerController::LeftMouseButtonUp()
{
    INPUT Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dx = 256; // If you need to set the position
    Input.mi.dy = 256; // If you need to set the position
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1, &Input, sizeof(INPUT));
}