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

Author Topic: Key event ALT + CONTROL  (Read 3825 times)

0 Members and 4 Guests are viewing this topic.

l00k

  • Newbie
  • *
  • Posts: 9
    • View Profile
Key event ALT + CONTROL
« on: April 01, 2016, 01:07:14 pm »
Hi all,

I'm using SFML 2.3.2 x32 and I have problem with key events.
When I press Alt + s to input "ś" (in Polish keyboard) I gets event with key = { code: 18, alt: true, control: true }
Control is wrongly set to true.
Is there any fix for that?

EDIT: Note that it's right alt. Left alt works fine.

BTW. I know about event TextEntered, but I pass events into external library (GWEN) and it checks control variable in order to perform special actions.

Code sample to debug error:
bool handleEvent(const sf::Event& event)
{
        if(event.type == 5)
                std::cout << event.key.code << " a: " << (int) event.key.alt << " c: " << (int) event.key.control << " s: " << (int) event.key.shift << "\n";

        return EventHandler::handleEvent(event);
}
« Last Edit: April 01, 2016, 02:00:25 pm by l00k »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Key event ALT + CONTROL
« Reply #1 on: April 01, 2016, 01:27:07 pm »
Please use sf::Event::KeyPressed and not a magic number. The assigned numbers are not guaranteed to be the same between different SFML versions.

The following code works fine for me.
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window({1000, 600}, "Test");
    window.setVerticalSyncEnabled(true);

    while(window.isOpen())
        {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                return 0;
            }
                        else if(event.type == sf::Event::KeyPressed)
                        {
                                std::cout << event.key.code << " a: " << (int) event.key.alt << " c: " << (int) event.key.control << " s: " << (int) event.key.shift << "\n";
                        }
        }

        window.clear();
        window.display();
    }
}
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

l00k

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Key event ALT + CONTROL
« Reply #2 on: April 01, 2016, 01:42:01 pm »
Right, This example is only for testing (not real implementation).

But what with this bug?
http://prntscr.com/amqwoe

EDIT: I took screen after compiling your code.
Could you send me binary? I would check it on my machine.

Also note that it occurs only with right alt. Left alt works fine.
« Last Edit: April 01, 2016, 02:02:11 pm by l00k »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Key event ALT + CONTROL
« Reply #3 on: April 01, 2016, 02:23:46 pm »
I'm not sure what keyboard layout you're using, but for my Swiss-German keyboard, the right Alt is marked with "alt gr". In many cases "alt gr" is just a substitute for ctrl + alt. You can test this yourself: If right alt + s = ś, then you can also press left ctrl + left alt + s and you might get ś.

Reading the Wikipedia article I'm now however not sure, if we can really subtitude alt gr as ctrl + alt....
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

l00k

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Key event ALT + CONTROL
« Reply #4 on: April 01, 2016, 02:36:12 pm »
I have the same layout.

SFML doc
http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1KeyEvent.php
bool alt : Is the Alt key pressed?
bool control : Is the Control key pressed?

I dont know cases when right alt ("right menu" in WinAPI) is in union with control, but IMO
these flags in event object should represent state of individual keys. Otherwise there sould be some abstraction layer over keyboard layout.

Maybe this sould be reported as an issue?

One more thing. You are probably more familiar in SFML than me. Could you help to find code fragment that I should change in SFML to avoid this behavior?
« Last Edit: April 01, 2016, 02:40:26 pm by l00k »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Key event ALT + CONTROL
« Reply #5 on: April 01, 2016, 02:49:43 pm »
Well the right alt is not just another alt key, it's an alt gr key and as said is often substituted for ctrl + alt.
The fact that SFML reports ctrl + alt on press alt gr is not an issue. The question I've been asking myself was more, whether this applies to all keyboard layouts. ;)

Regarding the SFML source code: It's pretty easy to go through it. The keyboard handling is part of the window module, so you can check the directories include/Window and src/Window. Since it's about event handling, it's most likely part of the Window class, so check the Window.hpp and Window.cpp. Since events are OS specific you'll have to dig into the specific OS implementation which are located in subdirectories of src/Window/XYZ.
« Last Edit: April 01, 2016, 02:52:02 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

l00k

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Key event ALT + CONTROL
« Reply #6 on: April 01, 2016, 02:56:47 pm »
So tell me how I should check for key combination: Alt + Ctrl + X for example. SFML gives me information that control is pressed but I have check again if control is really pressed. If it uses GetAsyncKeyState then I will get wrong results..

This is how it's done in SFML
Event event;
event.type        = Event::KeyPressed;
event.key.alt     = HIWORD(GetAsyncKeyState(VK_MENU))    != 0;
event.key.control = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
event.key.shift   = HIWORD(GetAsyncKeyState(VK_SHIFT))   != 0;
event.key.system  = HIWORD(GetAsyncKeyState(VK_LWIN)) || HIWORD(GetAsyncKeyState(VK_RWIN));
event.key.code    = virtualKeyCodeToSF(wParam, lParam);
pushEvent(event);
So WinAPI returns this info about control and alt. But I'm little confused - how to check key combination "Right Alt + Ctrl" in real?
« Last Edit: April 01, 2016, 03:14:02 pm by l00k »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Key event ALT + CONTROL
« Reply #7 on: April 01, 2016, 03:24:52 pm »
You can't.

SFML currently lacks the support for "unique key" identifier (see also issue #7).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

l00k

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Key event ALT + CONTROL
« Reply #8 on: April 01, 2016, 03:33:23 pm »
Ok, I see.
I will ignore control if alt is pressed.

Thanks for all replies.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Key event ALT + CONTROL
« Reply #9 on: April 01, 2016, 04:00:48 pm »
Note that the left and right can be treated separately using isKeyPressed() but not using an event's modifier flags.

I found that when pressing Right Alt ("Alt Gr" on my keyboard), it effectively "presses" both Right Alt and Left Control.

(click to show/hide)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything