SFML community forums

Help => Window => Topic started by: stuart88 on February 02, 2017, 03:19:03 pm

Title: "Key Pressed" event without pressing a key?
Post by: stuart88 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.
Title: "Key Pressed" event without pressing a key?
Post by: eXpl0it3r 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?
Title: Re: "Key Pressed" event without pressing a key?
Post by: stuart88 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.

(https://stuffthingsandopinions.files.wordpress.com/2015/10/proper-planning-prevents-piss-poor-performance-6.png)
Title: "Key Pressed" event without pressing a key?
Post by: eXpl0it3r 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.