Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Wrong key pressed in Linux Mint with XFCE 4.15.0-20-generic #21-Ubuntu (solved)  (Read 5893 times)

0 Members and 1 Guest are viewing this topic.

Edimartin

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Hi.
I am using SFML to make a game where the player can write text. I am making a text field to get the pressed keys from keyboard. I am having some problem with my ABNT2 keyboard.

I write this code for test:
#include <SFML/Graphics.hpp>
#include <SFML/Config.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    printf("\n%u %s %s SFML version == %u.%u.%u\n",__LINE__,__FILE__,__func__
           ,SFML_VERSION_MAJOR
           ,SFML_VERSION_MINOR
           ,SFML_VERSION_PATCH
           );fflush(stdout);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();

            //read the key pressed
            if(event.type == sf::Event::KeyPressed){
                printf("\n%u %s %s Key Pressed code == %d",__LINE__,__FILE__,__func__
                       ,event.key.code
                       );fflush(stdout);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Tilde)){
                    // left key is pressed: move our character
                    printf(" TILDE PRESSED");fflush(stdout);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Quote)){
                    // left key is pressed: move our character
                    printf(" QUOTE PRESSED");fflush(stdout);
                }
            }
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}
 

When I run the program, I press first the quote key '´' one time and then I press the tilde key '~' and I have this return:
$ ./helloSFML

9 main.cpp main SFML version == 2.5.1

28 main.cpp main Key Pressed code == -1 TILDE PRESSED
28 main.cpp main Key Pressed code == -1^C

Is this a problem with the last SFML version? Maybe I could try another old version.
How can I solve this problem?

I am using linux mint "4.15.0-20-generic #21-Ubuntu SMP Tue Apr 24 06:16:15 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux" with XFCE 4.12 with G++ "g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0".

Thanks.
« Last Edit: September 19, 2019, 07:02:22 pm by Edimartin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Wrong key pressed in Linux Mint with XFCE 4.15.0-20-generic #21-Ubuntu
« Reply #1 on: September 18, 2019, 09:06:15 pm »
For text input you should use the TextEntered event: https://www.sfml-dev.org/tutorials/2.5/window-events.php#the-textentered-event

Not all keys are detected properly with KeyPressed/KeyReleased and we're working on fixing that with scancode support. But you should never use these events for text input anyways
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Edimartin

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Wrong key pressed in Linux Mint with XFCE 4.15.0-20-generic #21-Ubuntu
« Reply #2 on: September 18, 2019, 10:11:23 pm »
Hi. Thanks for the explanation.
Unfortunately ti didn't work  :(. I press tilde two times and the ~ character appears but the '~' with 'a' to make 'ã' and it doesn't return.

I will test older versions of SFML.

Thank you very much.

Edimartin

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Hi eXpl0it3r.

As the SFML don't have this brazillian keys, i fixed this in the SFML source code in Linux. I hope you guys from the project can add it on the next version.

I found the X11 keys and I discover that keys are dead keys (don't know what does this mean).

Then I go on the file include/SFML/Window/Keyboard.hpp and I add this:
//here the add begin
        cCedilla,
        grave,
        acute,
        circumflex,
        tilde,
        macron,
        breve,
        abovedot,
        diaeresis,
        abovering,
        doubleacute,
        caron,
        cedilla,
        ogonek,
        iota,
        voicedSound,
        semivoicedSound,
        belowdot,
        hook,
        horn,
        //here the add end

        KeyCount,     ///< Keep last -- the total number of keyboard keys

And I go on the file src/SFML/Window/Unix/WindowImplX11.cpp and I add this code in function keysymToSF:
case XK_9:            return sf::Keyboard::Num9;
            //Here the add begin
            case XK_ccedilla:     return sf::Keyboard::cCedilla;
            case XK_cedilla:      return sf::Keyboard::cedilla;
            case XK_dead_grave:   return sf::Keyboard::grave;
            case XK_dead_acute:   return sf::Keyboard::acute;
            case XK_dead_circumflex: return sf::Keyboard::circumflex;
            case XK_dead_tilde:   return sf::Keyboard::tilde;
            case XK_dead_macron:  return sf::Keyboard::macron;
            case XK_dead_breve:   return sf::Keyboard::breve;
            case XK_dead_abovedot: return sf::Keyboard::abovedot;
            case XK_dead_diaeresis: return sf::Keyboard::diaeresis;
            case XK_dead_abovering: return sf::Keyboard::abovering;
            case XK_dead_doubleacute: return sf::Keyboard::doubleacute;
            case XK_dead_caron:   return sf::Keyboard::caron;
            case XK_dead_cedilla: return sf::Keyboard::cedilla;
            case XK_dead_ogonek:  return sf::Keyboard::ogonek;
            case XK_dead_iota:    return sf::Keyboard::iota;
            case XK_dead_voiced_sound: return sf::Keyboard::voicedSound;
            case XK_dead_semivoiced_sound: return sf::Keyboard::semivoicedSound;
            case XK_dead_belowdot: return sf::Keyboard::belowdot;
            case XK_dead_hook:    return sf::Keyboard::hook;
            case XK_dead_horn:    return sf::Keyboard::horn;
            //here the add end
        }

With this code, the events return the code of the key and not -1.

Thanks for the help.

PS: I merge two cedilla in one because one is dead and another is not. It's because I don't know hou SFML will have it in the future. I think my keyboard shoud use it but it use's the cCedilla (cedilla in the C letter).
I hope you can fix it creating two different cedillas.
« Last Edit: September 19, 2019, 02:36:14 am by Edimartin »

Edimartin

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Hi eXpl0it3r.
I make a mistake about the character tex from SFML.

The link you send have a code with testing if the character is smaller than 128, but the special UTF8 characters are bigger than 128.
I change it and it solved my problem.

Thanks. ;D