Archive 17/01/2023.

No keyboard input

pirogronian

I wrote simple app based on web sample and realized it doesnt detect keyboard input (I didn’t tested mouse and touch yet). Simplest code I made:

class Sim3D : public Application
{
    Scene *scene_;
    Node *cameraNode_;
public:
    Sim3D(Context *c) : Application(c) {}
    void Setup() override
    {
        engineParameters_["Fullscreen"] = false;
        engineParameters_["WindowWidth"] = 1024;
        engineParameters_["WindowWidth"] = 720;
        engineParameters_["WindowResizable"] = true;
    }

    void OnKeyDownDebug(StringHash evType, VariantMap &evData)
    {
        int key = evData[KeyDown::P_KEY].GetInt();
        String name = GetSubsystem<Input>()->GetKeyName(key);
        URHO3D_LOGINFO(name);
    }

    void Start() override
    {
        SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(Sim3D, OnKeyDownDebug));
    }
};

URHO3D_DEFINE_APPLICATION_MAIN(Sim3D)

Above is a test code I wrote after realizing GetKeyDown, called inside E_UPDATE handler, always return false. Handler itself was called properly.
No matter what I press, there is no output into console, except standard startup infos.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

if (COMMAND cmake_policy)
    # Libraries linked via full path no longer produce linker search paths
    cmake_policy (SET CMP0003 NEW)
    # INTERFACE_LINK_LIBRARIES defines the link interface
    cmake_policy (SET CMP0022 NEW)
    # MACOSX_RPATH is enabled by default
    cmake_policy (SET CMP0042 NEW)
    cmake_policy (SET CMP0063 NEW)
endif ()

project(Sim3D)

set (CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/Modules)

include (UrhoCommon)
# Define target name
set (TARGET_NAME Sim3D)
# Define source files
define_source_files (main.cpp)
# Setup target with resource copying
setup_main_executable ()
Modanung

I think the problem is a missing URHO3D_OBJECT(Sim3D, Application) in the class declaration.

Also, welcome to the forums! :confetti_ball: :slightly_smiling_face:

pirogronian

Thank You for fast reply. Indeed, documentation mentiones it, but sample I used no.
The good news are there is no errors.
The bad nes are there is also no result… :cry:
Could it be related to SDL configuration or something?

Edit: I run Urho3DPlayer. It clearly reacts on keyboard input, so it’s not system issue.

Modanung

All samples derive from the Sample class:

pirogronian

But not sample I used :smile:
I used https://github.com/urho3d/Urho3D/wiki/First-Project
and also looked at https://github.com/damu/Urho-Sample-Platformer/blob/master/main.cpp

TheComet

Events can only be received by class instances that inherit from Urho3D::Object and have the URHO3D_OBJECT macro. You need to add this to your sample:

class Sim3D : public Application
{
    URHO3D_OBJECT(Sim3D, Application)

    // ...
};```
Modanung

I’m surprised the sample platformer does not contain this.
There is this though:

Could it be related?

pirogronian

I manually changed directory to bin and then called ./Sim3D. But still no result.

Edit I added additional debug checks:

auto input = GetSubsystem<Input>();
if (input->HasEventHandlers())
    URHO3D_LOGINFO("Input has handlers.");
else
    URHO3D_LOGINFO("Input has no handlers!");
if (context_ == input->GetContext())
    URHO3D_LOGINFO("Input has the same context.");
else
    URHO3D_LOGINFO("Input has different context!");
VariantMap evdata;
evdata[KeyDown::P_KEY] = KEY_A;
SendEvent(E_KEYDOWN, evdata);

But output is as supposed to be:

[Wed Aug 28 19:55:03 2019] INFO: Input has handlers.
[Wed Aug 28 19:55:03 2019] INFO: Input has the same context.
[Wed Aug 28 19:55:03 2019] INFO: A

Edit2:
So far, hacking a bit, I checked that Sim3D is able to receive E_SDLRAWINPUT from Input object every time key is pressed and released, while E_INPUTBEGIN and E_INPUTEND are sent repeatedly all the time.

Modanung

Did you try adding URHO3D_OBJECT(Sim3D, Application) to the Sim3D class declaration?

pirogronian

Did you try adding URHO3D_OBJECT(Sim3D, Application) to the Sim3D class declaration?

It was the very first thing I did.

Edit: There are some rumors over the Internet about strange SDL 2.0.5 keyboard handling, what caused problems in Urho3D around 2016. I looked at my Urho3D package release date and it is around 2017, with very first 1.7 version. Maybe It’s a already fixed bug. I’ll try to build it myself from git master.

Modanung

Ah yes, this is indeed best (and common) practice. :slightly_smiling_face:

Leith

Oh hi man, did you subscribe to receive the event?
Welcome to the forum!
Don’t worry, we can work it out, but you might want to message me privately, and then post your solution back here :wink:
SubscribeToEvent(E_KEYDOWN, Urho3D_Handler( classname, methodname ));

Modanung

@Leith The code in the first post contains that line, yes.

Leith

Yeah, something more simple.
Most bugs are simple. This one certainly.
The input system works perfectly.
Thanks, SDL.

pirogronian

Rebuilding whole package from git master fixed the problem.

The only tiny issue I found was need of explicit cast int to Key in GetKeyName(). It should be done more elegant :wink:

Modanung

Could this be your first PR? :wink:

I’m also curious whether URHO3D_OBJECT(Sim3D, Application) made any difference in the end.

pirogronian

Yes, it did. My another class, not included in above test code, indeed didnt receive any events without it.

Edit:

Maybe, and surely for another topic, because I got very strange behaviour of Variant::Get<>() method…

Leith

Variant is a rubber typed container (basically a Union) - when receiving a variant, always check that the type is not VAR_NONE at minimum, and is your expected type at best.

pirogronian

Actually my problem was that Get<>() called from non inline function results in undefined reference error. But after some search over the Internet I think it’s nit an Urho3D bug but a c++ feature… :slight_smile:

Leith

compilers have their quirks, yep!