keybinding question

More
09 Jan 2018 21:54 #104218 by Grotius
keybinding question was created by Grotius
Hi,

Is there a tutorial for keybinding for stand alone linuxcnc application?

What i have tried to copy from Gmocappy :
def on_key_event( self, widget, event, signal ):
        # get the keyname
        keyname = gtk.gdk.keyval_name( event.keyval )
        print("pressed key = ",keyname)

        # estop with F1 shold work every time
        # so should also escape aboart actions
        if keyname == "F1":  # will estop the machine, but not reset estop!
        self.command.state( linuxcnc.STATE_ESTOP )

This give no error's but nothing is happening when pressing F1. I think i forgot some lines. Also i get no print message.
This is really a difficult item to solve for me.

Please Log in or Create an account to join the conversation.

More
10 Jan 2018 00:30 #104221 by cmorley
Replied by cmorley on topic keybinding question
probably need to see more of the code.
I assume you are using GTK?
In Gscreen and Gmoccapy there is code similar to this:

self.widgets.window1.connect('key_press_event', self.on_key_event,1)
self.widgets.window1.connect('key_release_event', self.on_key_event,0)

and that connects to:
def on_key_event(self,widget, event,state):
        """This catches all key events for Gscreen.
            if will ignore the initial press of shift keys.
            The last key press and its state is help in data.key_event_last.
            This will call a key lookup function that returns a function name.
            then that function is called with the key state info attached.
            If there is a function named in the handler file it will be called
            instead of the default function.
            Setting Gscreen in verbose mode will show key presses / state.
            Any errors in the key look up or function calls are trapped and ignored.
            Setting Gscreen into debug mode will show the errors.
        """
        CNTRL = SHIFT = ALT = 0
        keyname = gtk.gdk.keyval_name(event.keyval)
        self.verbosely_print("Key %s (%d) was pressed state: %d last: %s" % (keyname, event.keyval,state, self.data.key_event_last))
        if event.state & gtk.gdk.CONTROL_MASK:
            CNTRL = 1
            self.verbosely_print("Control was being held down")
        if event.state & gtk.gdk.MOD1_MASK:
            ALT = 1
            self.verbosely_print("Alt was being held down")
        if event.state & gtk.gdk.SHIFT_MASK:
            SHIFT = 1
            self.verbosely_print("Shift was being held down")
        if keyname in( "Shift_L","Shift_R"): return True # ignore shift key press
        if self.data.key_event_last[0] == keyname and self.data.key_event_last[1] == state : return True
        self.data.key_event_last = keyname,state
        try:
            method = self.keylookup.convert(keyname)
            if method:
                try:
                    try:
                        return self.handler_instance[method](state,SHIFT,CNTRL,ALT)
                    except:
                        self.show_try_errors()
                        return self.handler_instance.keybindings[method](state,SHIFT,CNTRL,ALT)
                except:
                    self.show_try_errors()
                    return self[method](state,SHIFT,CNTRL,ALT) 
        except:
            self.show_try_errors()
This calls a keybinding library (from gscreen import keybindings) that makes it easier to connect key codes to function calls.

Obviously your code would be different but similar.
so you must connect a key_press_event to your top window to catch keypress events and send them to a function
that will decide what to do after that.

Keypress code is difficult to get right in every situation.

Chris M
The following user(s) said Thank You: Grotius

Please Log in or Create an account to join the conversation.

More
10 Jan 2018 19:57 #104251 by Grotius
Replied by Grotius on topic keybinding question
@Chris,

Thank you for the comment's.

Is it possible that you help me or someone who can do this?

The purpose is writing a button example in my program code?

Today i was trying this for several hours with no succes.

I have also a second difficult item to solve. That is overwrite a dro value in a textbox. This option is included in Mach3 and is very
handy to use. In linux i tried this with parameter input with no succes.

For this help i will make a financial support.

Please Log in or Create an account to join the conversation.

More
10 Jan 2018 21:26 #104253 by cmorley
Replied by cmorley on topic keybinding question
Can you post your program?

Please Log in or Create an account to join the conversation.

More
10 Jan 2018 23:00 - 10 Jan 2018 23:02 #104260 by Grotius
Replied by Grotius on topic keybinding question
@Chris,

Yes of course.

grotius.py in usr/bin folder
grotius.glade in usr/share/linuxcnc/
rest of files in desktop/linuxcnc/configs/grotius/...

The glade window is not finished. but only for testing funtionality at this moment.
Attachments:
Last edit: 10 Jan 2018 23:02 by Grotius.

Please Log in or Create an account to join the conversation.

More
10 Jan 2018 23:04 - 10 Jan 2018 23:24 #104261 by Grotius
Replied by Grotius on topic keybinding question
@Chris,

Sorry double post.
Yes of course.

grotius.py in usr/bin folder
grotius.glade in usr/share/linuxcnc/
rest of files in desktop/linuxcnc/configs/grotius/...

The glade window is not finished. but only for testing funtionality at this moment.
Also the python code has to be cleaned up a little bit.

This program is based on the jt tutorials. Without the jt tutorial i was not able to come as far as now in a relative short time.

For now i like to have a keyboard binding function that works en can be expanded to move all axis.
Mostly i use the keyboard arrow's to jog x and y axis. Page up and page down for a-axis. Home and end for moving b-axis.

Good luck !! I hope this is gonna work. Keyboard buttons make life easyer !!

I tested the motor tuning already this week on a real plasma machine. It run's good. The motor sounds are little better then with mach.
It sounds like the stepgen pulses are cleaner. Maybe i am not right about that, but it's sounds like its better.

One of my dream's is to make a material library in the program for cutting steel and inox and alu.
The purpose is to select a material and thickness.
The rest of the parameters are automaticly transferred to the plasma inverter with motbus in the cutting program.
Also the thc value's is then read in from the modbuss.
A lot of customer's want this build in function in the software. It make's life easer for them.

This is one of my product's
Last edit: 10 Jan 2018 23:24 by Grotius.

Please Log in or Create an account to join the conversation.

More
11 Jan 2018 01:44 #104268 by cmorley
Replied by cmorley on topic keybinding question
Ok key events work.
Escape, f1 f2 and f4 do quit, estop, machine on , halmeter respectively

I think using Gscreen and gladevcp widgets would have been easier but then again I'm very familiar with them.

Chris M
Attachments:
The following user(s) said Thank You: Grotius

Please Log in or Create an account to join the conversation.

More
12 Jan 2018 00:14 - 12 Jan 2018 00:30 #104319 by Grotius
Replied by Grotius on topic keybinding question
Hi Chirs many thanks for the solution. I want to give you a financial support if that is possible.

I have added a key for moving the x-axis, this is working. Only difficult thing is to stop the x-axis.
The key release event is not as easy as it looks like. I tried several things, but maybe you have a quick solution for this.

This is within the setup keybinding :
self.keylookup.add_conversion('Right','JOGX','on_keycall_JOGXPLUS')

This is within the key call functions :
def on_keycall_JOGXPLUS(self,state,SHIFT,CNTRL,ALT):
       if state:
            self.on_jog_x_plus_pressed(None)
            return True

So if i delete "if state:" + "return True", the output to the machine is the stil the same.
If i change "return True" to "return False" nothing is changing too.
def on_keycall_JOGXPLUS(self,state,SHIFT,CNTRL,ALT):
            self.on_jog_x_plus_pressed(None)

Now if i add "self.on_jog_x_plus_released(None)" the machine is moving and then stopped directly. A sort of shocking effect.
def on_keycall_JOGXPLUS(self,state,SHIFT,CNTRL,ALT):
             self.on_jog_x_plus_pressed(None)
             self.on_jog_x_plus_released(None)
Last edit: 12 Jan 2018 00:30 by Grotius.

Please Log in or Create an account to join the conversation.

More
12 Jan 2018 00:38 #104322 by cmorley
Replied by cmorley on topic keybinding question
I forgot to tell you there are some keybinding built in so you don't need to add bindings:
F1                      on_keycall_ESTOP
F2                      on_keycall_POWER
escape             on_keycall_ABORT
up                      on_keycall_XPOS
down                 on_keycall_XNEG
right                    on_keycall_YPOS
left                       on_keycall_YNEG
page up             on_keycall_ZPOS
page down             on_keycall_ZNEG
bracket left             on_keycall_APOS
bracket right             on_keycall_ANEG
i                            on_keycall_INCREMENTS

Of course you can change them if you wish to.

Ok so jogging - it's the state of the button thats important.
true for pressed false for released, so pass that information to your jog function.
and have the job function stop the jog if it's false
def on_keycall_JOGXPLUS(self,state,SHIFT,CNTRL,ALT):
       if state:
            self.on_jog_x_plus_pressed(None, state) # <---look here
            return True

Please Log in or Create an account to join the conversation.

More
12 Jan 2018 00:47 #104323 by Grotius
Replied by Grotius on topic keybinding question
Jes,

I solved the problem :
if keyname == "Right" and state:
            self.command.jog(self.cnc.JOG_CONTINUOUS, 0, self.builder.get_object("jog_speed").get_value()/60)
        if keyname == "Right" and not state:
            self.command.jog(self.cnc.JOG_STOP, 0)

Please Log in or Create an account to join the conversation.

Time to create page: 0.225 seconds
Powered by Kunena Forum