How to make a Keyboard Cuts Manager | Journal #11
Hello Fellow python programmers do you want to help create shortcuts for gamers and friends? Today we will give a simple tutorial of my code.
Please understand that I’m talking through my code and not giving a step-by-step tutorial. You will get my code at the end.
Features
The Features are very simple (unlike usually). Lets start.
- Open a screen. Quite simple, when you start the program you want a window to open -the application- then begin what we told it to do after.
- Choose a framework. What framework will we use? Tkinter, pygame?
- Create command views. Change colours, size, name of letter, shift, ctrl, alt, meta/cmd.
- Link commands. Show the letter, shift, ctrl, alt, meta/cmd.
- Input commands. Input the letter, shift, ctrl, alt, meta/cmd.
- Add text. Add text to help user know what to do in the application.
Lets Begin coding!
Firstly, we will Choose a framework. To have a clean software, we will use flet as our chosen framework. We need to take these things from flet: Page, Row, Text, KeyboardEvent.
Page: The window.
Row: What row the text will be on.
Text: Text in the window.
KeyboardEvent: The keys we have pressed.
Next, we will open the window. However, as you open the window after the code we will type in, we have to instruct how the page looks.
Title: Name of window.
Spacing: Spacing between stuff.
Alignments: Where everything is displayed — in this case its centre.
page.title = "Keyboard Cuts"
page.spacing = 30
page.vertical_alignment = 'center'
page.horizontal_alignment = 'center'
Third, we will show the text views. This is very simple, we just do the same thing for every type of view and change some values. We make a different variable for every different view.
key: Text = Text(value='Key', size=30)
shift: Text = Text(value='Shift', size=30, color='red')
ctrl: Text = Text(value='Ctrl', size=30, color='yellow')
alt: Text = Text(value='Alt', size=30, color='green')
meta: Text = Text(value='Meta', size=30, color='blue')
Lets continue with handling the keyboard events.
We will make a new function called on_keyboard
def on_keyboard(e: KeyboardEvent) -> None:
key.value = e.key
shift.visible = e.shift
ctrl.visible = e.ctrl
alt.visible = e.alt
meta.visible = e.meta
print(e.data)
page.update()
Now that we know how to announce the events and add views we just need to link it to the window.
page.on_keyboard_event = on_keyboard
Now we have to create the page itself. This is actually very simple and is not a lot to code. We add the Text and then using the row we got from importing flet, we add the text views then align it in the center
page.add(
Text('Press any combination of keys to have them appear on the screen'),
Row(controls=[key, shift, ctrl, alt, meta], alignment=ft.MainAxisAlignment.CENTER)
)
Lastly, we set flet’s app target to main to command the code to only focus on that.
if __name__ == '__main__':
ft.app(target=main)
Please note that all of this has not been laid out properly and may end up in giving an error. Please go to the GitHub page to see the full code.
OUTPUT
Here is the full output when you use the program.
The window:
It doesn't just have to be A + Shift + Ctrl + Alt + Meta try doing other combinations. If it doesn’t work try adding a letter and not Shift, Ctrl, Alt or Meta. E.g Ctrl + Meta wont work But C + Ctrl + Meta will work — doesn’t have to be c.
Terminal Output
The Terminal gives you a message every time someone gets a combination.
tof = true or false
Example: "key":"A","shift":true,"ctrl":true,"alt":true,"meta":true}
Format: "key":"Letter","shift":tof,"ctrl":tof,"alt":tof,"meta":tof
Thanks For using this tutorial but before you leave i would like to introduce:
Hello everyone. I would like to inform you about my game Jump Up. This is my first ever game that i have made and would make my world if you download it. The link is down below as an itch.io website. Please note that as this game is in early versions (1.0.2) this game will not be available on windows, linux, ios, and some more. The only platform this game is available on at this moment in time is: Macbook. Thanks for reading this and please give my game a go and leave some feedback. Thank you everyone, JDJ out.
Get Jump Up ↑ by Junior Dev Journal: Here (Not a straight download link)
Links
Subscribe to JDJ: SUBSCRIBE!
Latest Blog: Here!
GitHub (Keyboard Cuts Manager): Journal #11
AD:
Best Trading Platform: Its True!
Get Jump Up ↑ by Junior Dev Journal: Here (Not a straight download link)