Pullusb / snippetsLibrary

Blender addon - personnal snippets library in text editor
GNU General Public License v3.0
35 stars 6 forks source link

Key press event snippet #4

Closed xan2622 closed 4 years ago

xan2622 commented 4 years ago

Hi.

It would be cool if your Snippets Library could feature a script that detects key presses events.

I think that this kind of code is often used by addons developers, it would be convenient to have such code in the Snippets Library.

Maybe this script can be used as inspiration for this Snippet: https://github.com/jayanam/shortcut_VUr/blob/master/scv_types.py

Pullusb commented 4 years ago

I only create snippets when I have an immediate use. I may have an old script that handles user input in modal but it's not as complete as Jayanam version. Maybe I'll directly add his script to the library... Or maybe YOU can propose a nice, well documented snippet for this ! 😉 what do you think

Anyway thank you for the contribution effort !

KoreTeknology commented 4 years ago

hi Sam, you are right, i think the first idea was to get a tool to include any type of code blocks assets, as you can create your own or import any other sources ;) thats the way i am using this tool thx for this contribution,

xan2622 commented 4 years ago

Or maybe YOU can propose a nice, well documented snippet for this! What do you think?

I wish I had a decent Python knowledge to create my own "Key press event" script, but I don't (I am working on it though, I'm learning).

It might worth reusing Jayanam's code if it works out of the box (testing the add-on makes me think it works fine).

Pullusb commented 4 years ago

@xan2622 I took some time to make a documented modal template for keypress handle (And another for keypress handling with basic drawing in viewport). These are working standalone more than snippets so I putted them in my ops category. https://github.com/Pullusb/snippetsLibrary/tree/master/snippets/ops It's adapted from one of my previous addon. Certainly not as complete as jayanam code but I prefer to publish something I made and used myself (and moreover... that I undestand ^^).

xan2622 commented 4 years ago

Thank you so much.

It's funny (a coincidence) that you replied and shared these scripts because I have been (for the last days) working on a script that catches key press events.

I haven't finished yet, it's still a very early WIP (I am still trying to figure out what I am adding in my code, lol). I am still learning python..

I notice that you use def invoke(self, context, event):. I added it too in my script. 💪

I have gave up a bit on getting inspired by shortcut_VUr, it's a bit too complicated for my knowledge but I will certainly take a look at your code (which is well commented btw, thanks!).

Pullusb commented 4 years ago

I'm happy that it's usefull to you. I wanted to make it earlier but been very busy.

In a modal (as well in other operator) the invoke is triggered first, before the execute. So usually this is where I run some check with the state of the sene and initialise class variable (self.variable) if any needed. If you don't need detection of ctrl/alt/shift you can erase a good amount of code in the snippet.

Out of curiosity... 🙄 What are you working on ? Will it be a public tool ?

xan2622 commented 4 years ago

I hope to be able to make it public but for the moment, I prefer to keep it secret, as long as it's not finished. Hint: it's not going to be an amazing addon like BOXCutter or Bezier Utilities. For the moment, I am just trying to understand python and make my own scripts.