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

Author Topic: "Key Pressed" event without pressing a key?  (Read 2976 times)

0 Members and 1 Guest are viewing this topic.

stuart88

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
"Key Pressed" event without pressing a key?
« on: February 02, 2017, 03:19:03 pm »
I have two cases where I want something to happen which is equivalent to a key press, without that key being pressed.

In the case of the code I've written, I'd like to make it so that pressing 'Space' runs the same code as pressing 'R'.

Secondly, I have a problem wherein I'd like to initiate the equivalent of a key press after a certain amount of time has elapsed. Something like:

if(elapsedtime % 5 != 0)
{
Tell the program to run the code where the "S" key was pressed
}

I know I could just copy and paste the code blocks into the area they're needed, but I still wonder if there's another way to do this. Having two huge chunks of the same code feels a bit messy.

Many thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
"Key Pressed" event without pressing a key?
« Reply #1 on: February 02, 2017, 03:43:38 pm »
Do you mean like using a function and then calling that function from the different code places?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

stuart88

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: "Key Pressed" event without pressing a key?
« Reply #2 on: February 02, 2017, 04:35:09 pm »
Hmm kind of.

On second thought, I think the problem is my own fault. I wrote a ton of code directly for each keypress, rather than directing them to functions elsewhere.

The 6 P's strike again, heh.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
"Key Pressed" event without pressing a key?
« Reply #3 on: February 02, 2017, 06:44:35 pm »
Just do a refactoring. :)

Structuring code nicely from the start is a complex task, that requires a lot of experience.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/