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

Author Topic: Trapping function keys  (Read 6426 times)

0 Members and 1 Guest are viewing this topic.

Ectance

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trapping function keys
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trapping function keys
« Reply #1 on: November 24, 2011, 09:43:26 pm »
Could you please provide a complete and minimal example that reproduces the problem?
Laurent Gomila - SFML developer

Ectance

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trapping function keys
« Reply #2 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trapping function keys
« Reply #3 on: November 24, 2011, 09:54:51 pm »
Laurent Gomila - SFML developer

Ectance

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trapping function keys
« Reply #4 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

Ectance

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trapping function keys
« Reply #5 on: November 25, 2011, 01:19:01 pm »
no one ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trapping function keys
« Reply #6 on: November 25, 2011, 01:33:29 pm »
I'll test it as soon as I can.
Laurent Gomila - SFML developer

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Trapping function keys
« Reply #7 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trapping function keys
« Reply #8 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.
Laurent Gomila - SFML developer

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Trapping function keys
« Reply #9 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)

GAFBlizzard

  • Newbie
  • *
  • Posts: 4
    • View Profile
Trapping function keys
« Reply #10 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.  :)

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Trapping function keys
« Reply #11 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 ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trapping function keys
« Reply #12 on: February 07, 2013, 10:37:18 am »
Nothing new, my last reply still applies.

And yes, the given solution is the good one.
Laurent Gomila - SFML developer

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Trapping function keys
« Reply #13 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trapping function keys
« Reply #14 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
Laurent Gomila - SFML developer

 

anything