SFML community forums

Help => Window => Topic started by: Riton_Lafouine on May 24, 2020, 12:51:05 pm

Title: Can't make CTRL + mouse wheel work
Post by: Riton_Lafouine on May 24, 2020, 12:51:05 pm
Hello,

I'm sure i'm doing something stupid, i tried many combinations without success, certainly a syntax mistake but can't figure out what do do

the problem is that the
if (event.key.control)
is always true event if ctrl key is not pressed

Here is part of my code :

case sf::Event::MouseWheelMoved:
        {
            if(sf::Mouse::HorizontalWheel)
            {
                int8_t delta = event.mouseWheel.delta;
                std::cout << "wheel movement: " << delta << std::endl;
                for(uint16_t i=0; i<selected_chan.size() ;i++)
                {
                    if(ch(selected_chan[i])get_channel_number()!=0)//si le circuit a ete cree
                    {
                        uint8_t res = 0;
                        uint8_t niv = ch(selected_chan[i]) get_level();

                        if(ch(selected_chan[i])get_mode()==false)
                        {
                            if(event.key.control)
                            {
                                cout << "control" << endl;
                                res = 2.55;
                            }
                            else
                            {
                                res = scroll_resolution*2.55;
                            }

thanks by advance for your answer
Title: Re: Can't make CTRL + mouse wheel work
Post by: Athenian Hoplite on May 24, 2020, 01:03:02 pm
The sf::Event class stores its members in a union. If a sf::Event is of type sf::Event::MouseWheelMoved then it's defined member is "event.mouseWheel". To access the member "event.key" you need to have an event that is of type "sf::Event::KeyPressed" (or released).

A union is a region of memory that can be interpreted in different ways. When you access a member in a union you are requesting that memory to be given to you in that "interpretation". So although you are getting event.key the memory is actually meaningless because the memory stored in the union is for event.mouseWheel. Because of a stroke of luck it just so happens that when reading event.key the memory is layed out in such a way that it reads "true" for event.key.control.

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Event.php

To achieve what you want you first need to catch a sf::Event::KeyPressed and check to see if control is pressed (or not) and store that somewhere. Then in that code that you posted you need to check if your variable in which you stored if control is pressed is true.
Title: Re: Can't make CTRL + mouse wheel work
Post by: Riton_Lafouine on May 24, 2020, 01:06:32 pm
OK, thank you very much !!! i'll do that

***EDIT***
works like a charm and will be usefull in other places of the code also... many thanx

Regard,

RLF