Hello,
I created an offline version of the functionality you have asked for, If you want to convert it to an online experiment, it should be relatively easy; you will need to change the keyboard.
But what is important here is the functionality.
The routine:
First of all, at the start of the routine, we set the following code:
kb = keyboard.Keyboard()
toggleHours = True
hours = []
for i in range(24):
hours.append(str(i).rjust(2, '0'))
hourCounter = 0
minutes = []
for j in range(60):
minutes.append(str(j).rjust(2, '0'))
minuteCounter = 0
kb
- Set the keyboard
toggleHours
- Toggle the control between the hours and the minutes
hours
and minutes
- An array with all the minutes and hours, e.g. [“00”, “01”, “02”, “03”…]
hourCounter
and minuteCounter
- Counting the number of presses for the minutes and hours.
Then, we set the following in Every Frame:
keys_pressed = kb.getKeys(['up', 'down', 'right', 'left'], waitRelease=False, clear=True)
for key in keys_pressed:
if 'right' == key or 'left' == key:
toggleHours = not toggleHours
if toggleHours:
if 'up' == key:
hourCounter= (hourCounter - 1) % 24
elif 'down' == key:
hourCounter= (hourCounter + 1) % 24
else:
if 'up' == key:
minuteCounter= (minuteCounter - 1) % 60
elif 'down' == key:
minuteCounter= (minuteCounter + 1) % 60
We define the keys pressed in each frame (keys_pressed
).
We then iterate through the pressed keys (because the .getKeys
method returns an array).
If the right/left key were pressed, we toggle the toggleHours variable. When it’s true, we control the hours; when it’s false, we control the minutes.
When the up/down key is pressed, we set the hourCounter
to be equal to itself minus/plus one. We also use the %
operator to get the remainder of the hourCounter
(for example, if the down key was pressed 24, we want to return to 0). The same logic goes to the minuteCounter
.
So now, after we define our counters, we can return to the routine and set the components.
I will take the hours components as an example:
For the hour component, we want to take an hour, which is in the hourCounter
position in the hours
array.
So if our counter equals 5, we will take the sixth item from the hours
array, which is “06”.
For the hour_plus
and hour_minus
components, we use the same logic, but we add/subtract one:
We also need to use the
%
operator again in case the
hourCounter
is out of range.
And there you go:
Hope it helps,
Chen