sciter-sdk / pysciter

Python bindings for Sciter
https://sciter.com
MIT License
396 stars 40 forks source link

Key Events not detected #32

Closed martinlombana closed 3 years ago

martinlombana commented 3 years ago

Hi,

Following your advice, I've subscribed to ALL the events, but the KEY_DOWN or up, etc, are not showing. Even when I subscribe ONLY to the KEYS, they don't get registered. As a side note, if it were to work, how can I recover the KEY_CODE?

Thanks!

import ctypes

import sciter
from sciter import SCITER_RT_OPTIONS, Element
from sciter.capi.scbehavior import EVENT_GROUPS

class RootEventHandler(sciter.EventHandler):
    def __init__(self, el, frame):
        super().__init__(element=el, subscription=EVENT_GROUPS.HANDLE_ALL)
        self.parent = frame
        pass

    def on_event(self, source, target, code, phase, reason):
        he = sciter.Element(source)
        print('CODE-> ', code, flush=True)

        if code == sciter.event.KEY_EVENTS.KEY_DOWN:
            print('KEY DOWN')

        pass

class Frame(sciter.Window):

    def __init__(self):
        super().__init__(ismain=True, uni_theme=True, debug=False, size=(500, 500), resizeable=False)
        self.set_dispatch_options(enable=True, require_attribute=False)
        pass

if __name__ == "__main__":
    print("Sciter version:", sciter.version(as_str=True))
    sciter.set_option(SCITER_RT_OPTIONS.SCITER_SET_DEBUG_MODE, True)
    frame = Frame()
    frame.setup_debug()
    # frame.load_file("index.html")

    frame.load_html(b"<html><body><p>I want to be substituted</p></body></html>")

    ev2 = RootEventHandler(frame.get_root(), frame)
    frame.run_app()
pravic commented 3 years ago

Ah, I see. on_event is for EVENT_GROUPS.HANDLE_BEHAVIOR_EVENT only.

We need an explicit handle_key: https://github.com/c-smile/sciter-sdk/blob/master/include/sciter-x-behavior.h#L810

The reason I haven't added all of the notifications is that it's easier to deal with them from TIScript. Of course, I can easily add the remaining (or you can make a PR).

For the reference: https://github.com/sciter-sdk/pysciter/blob/master/sciter/event.py#L194

martinlombana commented 3 years ago

I am a bit lost still with the C integration and I don't know, as much as you, the real inner workings of sciter (C version). If you could add the missing ones, whenever you have the time of course, I would be grateful. I still need a bit more of road time I think, and study the whole thing to master it, which I intend to do. I am "scited" about this great project :).

Thanks!

pravic commented 3 years ago

https://github.com/sciter-sdk/pysciter/blob/master/sciter/event.py#L115

code: BEHAVIOR_EVENTS and you are comparing it with code == sciter.event.KEY_EVENTS.KEY_DOWN: in the first post. You see?

I'll add the missing handlers. But you didn't answer the question: why don't you want to use TIScript for UI code and leave Python for high-level logic?

https://sciter.com/sciter-ui-application-architecture/

martinlombana commented 3 years ago

Oh, sorry @pravic, I didn't pick up your question.

My usual workflow is: --> Backend: Python (Django/Flask or pure Python) --> FrontEnd: TML5/JS or TypeScript (In the web, or packaged as an electron app)

Now, why do I want to use sciter as a 100% python solution? The answer is:

  1. There is no other UI lib for python that has the flexibility of HTML/CSS and the "power" of python ( I know C is more perfomant, but python is more accessible. I am not implying that python is faster, of course, because it is interpreted. Although we could compile some things with Cpython).

  2. I do not want to duplicate calls between JS/TisScript or to create a communication channel (albeit being easy). I want all the logic to be python (UI Events and server), and all the graphics to be HTML/CSS. It will simplify the development of Kiosk Applications (for my particular use case), as I will not have to go back and forth between python/JS. I tend to create a lot of templates and structures in order to be able to modify things at a "data-description" level (a json file, being the simplest example, and a JSON file from a rest-api, that can change being the more complex). Sometimes it's difficult to draw the line where JS/TIS should do some things, and where should python come into play. I am going to keep developing other systems with the other approach (but just for PWA apps). But here, given the fact that we can not use the full JavaScript language and with QuickJS neither, I can't load angular 8+, for instance. Nor I want it, because I want to create super perfomant kiosk apps that run with very little ressources. I can see, for what I've read about sciter, that @c-smile is very, very, very focused on performance and he has an advanced knowledge of lower level C/C++ apis. And I agree with that approach. Being able to load super complex frameworks here, is not my intented case. Quite the oposite. A want to "clean" myself from the complexity, dependency and slowliness, that some frameworks like angular bring to the table (although being useful for fast development, of course). Therefore, I am creating my own super simple CSS framework for UI (that will also be compatible with regular browsers) and for all the logic, I want to use python. It's also a personal choice to see if I can do it that way. I am always keen on trying difficult things (although I have still a lot to learn, specially in C or C++, which I haven't almost never touched).

  3. I also think that opening this way of doing things for sciter, could be good. It would be the only library (I don't know of other ones) that allows you to do this out of the box. And, in the future, as @c-smile mentioned, we could even compile the lib without tis or js support, thus reducing 1MB of the bundle (I am not worried about that overhead just right now). This could be a positive "marketing" move for sciter, as I know that many people are looking into creating nice UIs with python, but they do not want to add another language in the mix. And HTML/CSS is so simple (and modern!) that being able to modify it and control every aspect with python only looks very attractive. This is a much much better solution to tkinter, or other frameworks, where it is very frutrating to develop a simple well aligned and beautiful UI. Therefore, I have never seen a very nice modern looking tkinter or wxpython app... (There may be some, but they are quite difficult to find and also to create).

Sorry about the long explanation, I hope I answered your question :D.

Cheers!

martinlombana commented 3 years ago

Thanks! I will check it out soon :).