SFML community forums

Help => General => Topic started by: Ectance on November 24, 2011, 09:22:52 pm

Title: Trapping function keys
Post by: Ectance on November 24, 2011, 09:22:52 pm
i have this code :

Code: [Select]

while(true)
{
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Num1))
      smFunction = 0;
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Num2))
      smFunction = 1;
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Num3))
      smFunction = 2;
   std::cout<<smFunction<<std::endl;
}


and it works as expected , but if i try to get function key state the input get stucked :

Code: [Select]

while(true)
{
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F10))
      smFunction = 0;
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F11))
      smFunction = 1;
   if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F12))
      smFunction = 2;
   std::cout<<smFunction<<std::endl;
}


if i click on the screen rapidly the values are changing but if not , the value of <smFunction> get stuck at the previous value at a random time . also when stuck , i think the window loses focus for a second.

is this a bug or I'm doing it wrong ?

ps. im using sfml 2.0 ( 1-2max week snapshot ) on win7x32 machine
ps2. sorry for my english
Title: Trapping function keys
Post by: Laurent on November 24, 2011, 09:43:26 pm
Could you please provide a complete and minimal example that reproduces the problem?
Title: Trapping function keys
Post by: Ectance on November 24, 2011, 09:50:08 pm
Code: [Select]

void loop()
{
   smTerminate = false;
   smUiMode = SM_UIMODE_CONSOLE;

   while(!smTerminate && render_window->IsOpened())
   {
      switch(smUiMode)
      {
         case SM_UIMODE_CONSOLE:
            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F10))
            {
               std::cout<<"0\n";
               smConsoleActive = 0;
            }

            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F11))
            {
               std::cout<<"1\n";
               smConsoleActive = 1;
            }

            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F12))
            {
               std::cout<<"2\n";
               smConsoleActive = 2;
            }
           
            while (render_window->PollEvent(evt))
            {
               if(evt.Type == sf::Event::TextEntered)
               {
                  switch(evt.Key.Code)
                  {
                     case 100 :
                        // do stuff :
                        break;
                  }
               }
            }
            console[smConsoleActive].Display(render_window);
            break;



         default:
            ;
      }
   }
}


if i replace F10,F11 and F12 with Num1 Num2 and Num3 it works fine
Title: Trapping function keys
Post by: Laurent on November 24, 2011, 09:54:51 pm
Quote
complete

Quote
minimal

http://www.sfml-dev.org/forum/viewtopic.php?p=36368#36368
;)
Title: Trapping function keys
Post by: Ectance on November 24, 2011, 10:08:41 pm
Code: [Select]

#include <SFML/Config.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>



int main()
{
   sf::RenderWindow *render_window;
   sf::ContextSettings render_settings;
   unsigned char smConsoleActive = 0;
   sf::Event evt;

   render_settings.AntialiasingLevel  = 0;
   render_settings.DepthBits          = 0;
   render_settings.MajorVersion       = 2;
   render_settings.MinorVersion       = 0;
   render_settings.StencilBits        = 0;

   render_window = new sf::RenderWindow(sf::VideoMode(1024,768,32),"win",sf::Style::Titlebar | sf::Style::Close,render_settings);
   bool terminate = false;


   while(!terminate && render_window->IsOpened())
   {
      if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F10))
      {
         std::cout<<"0\n";
         smConsoleActive = 0;
      }

      if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F11))
      {
         std::cout<<"1\n";
         smConsoleActive = 1;
      }

      if(sf::Keyboard::IsKeyPressed(sf::Keyboard::F12))
      {
         std::cout<<"2\n";
         smConsoleActive = 2;
      }
           
      while (render_window->PollEvent(evt))
      {
         if(evt.Type == sf::Event::TextEntered)
         {
            switch(evt.Key.Code)
            {
               case 100 :
                  // do stuff :
                  break;
            }
         }
      }
      render_window->Clear();
      render_window->Display();
   }

   delete render_window;
   return 0;
}



so if you press fast the three F-keys the output in std::cout is wrong ~50% of the time
Title: Trapping function keys
Post by: Ectance on November 25, 2011, 01:19:01 pm
no one ?
Title: Trapping function keys
Post by: Laurent on November 25, 2011, 01:33:29 pm
I'll test it as soon as I can.
Title: Trapping function keys
Post by: julen26 on November 25, 2011, 05:05:09 pm
Yes, you're right, something goes wrong when typing those keys fast. I don't know what's going on.
Title: Trapping function keys
Post by: Laurent on November 25, 2011, 11:03:13 pm
The problem is with the F10 key on Windows. It has a special meaning for the OS (don't know which one though), and pressing it seems to enter a state where other keys are ignored until you press it again.

I can probably disable this special behaviour in SFML, but first I'd like to find what it's supposed to mean for the OS.
Title: Trapping function keys
Post by: Lo-X on November 26, 2011, 12:39:59 am
I don't know if it helps but I found this :
F10 :
 - In Microsoft Windows activates the menu bar of an open application.
 - Access the hidden recovery partition on HP and Sony computers.

(source) (http://www.computerhope.com/issues/ch000306.htm)
Title: Trapping function keys
Post by: GAFBlizzard on February 21, 2012, 08:42:35 am
See this page:

http://devmaster.net/forums/topic/8587-help-with-wm-keydown-sc-keymenu-f10-key-press/

I ran into this myself, and I solved this by changing the SFML library in WindowImplWin32::GlobalOnEvent as follows.

Before:
Code: [Select]
   // We don't forward the WM_CLOSE message to prevent the OS from automatically destroying the window
    if (message == WM_CLOSE)
        return 0;


After:
Code: [Select]
   // We don't forward the WM_CLOSE message to prevent the OS from automatically destroying the window
    if (message == WM_CLOSE)
        return 0;

    // We don't forward WM_SYSKEYDOWN and WM_SYSKEYUP messages for VK_F10 and
    // the alt keys to prevent Windows from pausing the game and trying to
    // activate a menu bar.
    if (((message == WM_SYSKEYDOWN) || (message == WM_SYSKEYUP))  &&
        ((wParam == VK_F10) || wParam == VK_MENU))
    {
        return 0;
    }


Laurent, feel free to use this in the official library code if you like, and it seems reasonable to you.  :)
Title: Re: Trapping function keys
Post by: gyscos on February 07, 2013, 10:30:22 am
Any news on this ? This is a pretty old thread, yet the issue is still present. Is the solution GAFBlizzard proposed a bad one ? If so, what would be a better one ? If not, what prevents it from being merged into the main repository ?
Title: Re: Trapping function keys
Post by: Laurent on February 07, 2013, 10:37:18 am
Nothing new, my last reply still applies.

And yes, the given solution is the good one.
Title: Re: Trapping function keys
Post by: gyscos on February 07, 2013, 10:55:58 am
I can probably disable this special behaviour in SFML, but first I'd like to find what it's supposed to mean for the OS.

Quote
F10 : In Microsoft Windows activates the menu bar of an open application.

It's pretty much the same behaviour as the "ALT" key.
Title: Re: Trapping function keys
Post by: Laurent on February 07, 2013, 11:10:04 am
There's a task in the tracker for this issue:
https://github.com/SFML/SFML/issues/261
Title: Re: Trapping function keys
Post by: GAFBlizzard on February 08, 2013, 02:25:19 pm
I can probably disable this special behaviour in SFML, but first I'd like to find what it's supposed to mean for the OS.

Quote
F10 : In Microsoft Windows activates the menu bar of an open application.

It's pretty much the same behaviour as the "ALT" key.
Agreed.  Pressing ALT highlights or unhighlights the menu bar, and pressing F10 also highlights or unhighlights the menu bar.  Laurent, if you want more reference info, here are a couple of links.

Keyboard shortcuts which mention F10:
http://support.microsoft.com/kb/126449

Reference about WM_KEYDOWN which mentions F10:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx