# ~ W = root_window.tk.call


##########################################################################################
##########     MODIFYING AXIS KEYBINDING MADE EASY BUT NOT TOO EASY   ##########
##########################################################################################


###   this file goes into the same folder as your config
###     add this line to your ini file, under the display section (without the #)
###     USER_COMMAND_FILE = usercommand_keybind.py

###   unbind a key first (if needed), then rebind  to your new thing, for example
###         the down key is unbound
#######    root_window.unbind('<Down>')
#          then rebind it to your new command
#          root_window.bind('<Down>', select_next)

####   this is for binding axis jog keys , the number at the end is the joint
#   bind right and left keys to joint 0, usually x
#   bind_axis("Right", "Left", 0)
#   bind down and up keys to joint 1, usually y
#   bind_axis("Down", "Up", 1)


#####   example for a user on the forum doing jog things


root_window.unbind("Prior")
root_window.unbind("Next")
root_window.unbind("Home")
root_window.unbind("End")
root_window.unbind("KP_Left")
root_window.unbind("KP_Right")
root_window.unbind("KP_Down")
root_window.unbind("KP_Up")
root_window.unbind("KP_Next")
root_window.unbind("KP_Prior")
root_window.unbind("KP_Home")
root_window.unbind("KP_End")
root_window.unbind("KP_7")
root_window.unbind("KP_1")
root_window.unbind("bracketleft")
root_window.unbind("bracketright")
bind_axis("Left", "Right", 2) #works
bind_axis("Up", "Down", 1) #works
bind_axis("Pg Up", "Pg Dn", 0)
bind_axis("4", "6", 3)
bind_axis("3", "9", 2)

#0=X 1=Y 2=Z 3=B 4=C

# the rough example i use in my usercommand file, but for our purpose it will work
#  uncomment the keys you want and make the changes
#  there are more keys than i have listed , easy but not too easy

#  ### UNBIND
#root_window.unbind("l")#, commands.toggle_override_limits)
#root_window.unbind("o")#, commands.open_file)
#root_window.unbind("s")#, commands.task_resume)
#root_window.unbind("t")#, commands.task_step)
#root_window.unbind("p")#, commands.task_pause)
#root_window.unbind("R")#, commands.task_reverse)
#root_window.unbind("F")#, commands.task_forward)
#root_window.unbind("v")#, commands.cycle_view)
#root_window.unbind("r")#, commands.task_run)
#root_window.unbind("B")#, commands.brake_on)
#root_window.unbind("b")#, commands.brake_off)
#root_window.unbind("x")#, lambda event: activate_ja_widget("x"))
#root_window.unbind("y")#, lambda event: activate_ja_widget("y"))
#root_window.unbind("z")#, lambda event: activate_ja_widget("z"))
#root_window.unbind("a")#, lambda event: activate_ja_widget("a"))
#root_window.unbind("c")#, lambda event: jogspeed_continuous())
#root_window.unbind("d")#, lambda event: widgets.rotate.invoke())
#root_window.unbind("i")#, lambda event: jogspeed_incremental())
#root_window.unbind("I")#, lambda event: jogspeed_incremental(-1))
#root_window.unbind("`")#, lambda event: activate_ja_widget_or_set_feedrate(0))
#root_window.unbind("1")#, lambda event: activate_ja_widget_or_set_feedrate(1))
#root_window.unbind("2")#, lambda event: activate_ja_widget_or_set_feedrate(2))
#root_window.unbind("3")#, lambda event: activate_ja_widget_or_set_feedrate(3))
#root_window.unbind("4")#, lambda event: activate_ja_widget_or_set_feedrate(4))
#root_window.unbind("5")#, lambda event: activate_ja_widget_or_set_feedrate(5))
#root_window.unbind("6")#, lambda event: activate_ja_widget_or_set_feedrate(6))
#root_window.unbind("7")#, lambda event: activate_ja_widget_or_set_feedrate(7))
#root_window.unbind("8")#, lambda event: activate_ja_widget_or_set_feedrate(8))
#root_window.unbind("9")#, lambda event: activate_ja_widget_or_set_feedrate(9))
#root_window.unbind("0")#, lambda event: activate_ja_widget_or_set_feedrate(10))
#root_window.unbind("!")#, "set metric [expr {!$metric}]; redraw")
#root_window.unbind("@")#, commands.toggle_display_type)
#root_window.unbind("#")#, commands.toggle_coord_type)
#root_window.unbind("$")#, commands.toggle_teleop_mode)
#root_window.unbind("<Home>")#, commands.home_joint)
#root_window.unbind("<End>")#, commands.touch_off_system)
#root_window.unbind(".")#, commands.toggle_coord_type)
#root_window.unbind(",")#, commands.toggle_teleop_mode)
#root_window.unbind(";")#, commands.toggle_coord_type)
#root_window.unbind("'")#, commands.toggle_teleop_mode)
    

#########bind
#root_window.bind("l", commands.toggle_override_limits)
#root_window.bind("o", commands.open_file)
#root_window.bind("s", commands.task_resume)
#root_window.bind("t", commands.task_step)
#root_window.bind("p", commands.task_pause)
#root_window.bind("R", commands.task_reverse)
#root_window.bind("F", commands.task_forward)
#root_window.bind("v", commands.cycle_view)
#root_window.bind("r", commands.task_run)
#root_window.bind("B", commands.brake_on)
#root_window.bind("b", commands.brake_off)
#root_window.bind("x", lambda event: activate_ja_widget("x"))
#root_window.bind("y", lambda event: activate_ja_widget("y"))
#root_window.bind("z", lambda event: activate_ja_widget("z"))
#root_window.bind("a", lambda event: activate_ja_widget("a"))
#root_window.bind("c", lambda event: jogspeed_continuous())
#root_window.bind("d", lambda event: widgets.rotate.invoke())
#root_window.bind("i", lambda event: jogspeed_incremental())
#root_window.bind("I", lambda event: jogspeed_incremental(-1))
#root_window.bind("`", lambda event: activate_ja_widget_or_set_feedrate(0))
#root_window.bind("1", lambda event: activate_ja_widget_or_set_feedrate(1))
#root_window.bind("2", lambda event: activate_ja_widget_or_set_feedrate(2))
#root_window.bind("3", lambda event: activate_ja_widget_or_set_feedrate(3))
#root_window.bind("4", lambda event: activate_ja_widget_or_set_feedrate(4))
#root_window.bind("5", lambda event: activate_ja_widget_or_set_feedrate(5))
#root_window.bind("6", lambda event: activate_ja_widget_or_set_feedrate(6))
#root_window.bind("7", lambda event: activate_ja_widget_or_set_feedrate(7))
#root_window.bind("8", lambda event: activate_ja_widget_or_set_feedrate(8))
#root_window.bind("9", lambda event: activate_ja_widget_or_set_feedrate(9))
#root_window.bind("0", lambda event: activate_ja_widget_or_set_feedrate(10))
#root_window.bind("!", "set metric [expr {!$metric}]; redraw")
#root_window.bind("@", commands.toggle_display_type)
#root_window.bind("#", commands.toggle_coord_type)
#root_window.bind("$", commands.toggle_teleop_mode)
#root_window.bind("<Home>", commands.home_joint)
#root_window.bind("<End>", commands.touch_off_system)




####################################################
#       this is the very slick keybinding thing used in plasmac2
#       posted for reference only

# ~ def keyboard_bindings(state):
    # ~ if firstRun:
        # ~ set_help_text()
    # ~ # delete kb shortcuts from help menu
    # ~ rC('.menu.help','delete',3,5)
    # ~ # remove current bindings
    # ~ for key in root_window.bind():
        # ~ root_window.unbind(key)
    # ~ # create new key bindings if enabled
    # ~ if state:
        # ~ # Escape
        # ~ root_window.bind('<Escape>', commands.task_stop)
        # ~ # F keys
        # ~ root_window.bind_class('all', '<Key-F1>', commands.estop_clicked)
        # ~ root_window.bind('<Key-F2>', commands.onoff_clicked)
        # ~ root_window.bind('<Key-F3>', pane_top + '.tabs raise manual')
        # ~ root_window.bind('<Key-F4>', commands.next_tab)
        # ~ root_window.bind('<Key-F5>', pane_top + '.tabs raise mdi')
        # ~ root_window.bind('<Key-F5>', '+' + tabs_mdi + '.command selection range 0 end')
        # ~ root_window.bind('<Key-F9>', commands.manual_cut)
        # ~ # axis selection
        # ~ root_window.bind('x', lambda event: activate_ja_widget('x'))
        # ~ root_window.bind('y', lambda event: activate_ja_widget('y'))
        # ~ root_window.bind('z', lambda event: activate_ja_widget('z'))
        # ~ root_window.bind('a', lambda event: activate_ja_widget('a'))
        # ~ root_window.bind('b', lambda event: activate_ja_widget('b'))
        # ~ #jogging
        # ~ root_window.bind('c', lambda event: jogspeed_continuous())
        # ~ root_window.bind('i', lambda event: jogspeed_incremental())
        # ~ root_window.bind('I', lambda event: jogspeed_incremental(-1))
        # ~ bind_axis('Left', 'Right', 0)
        # ~ bind_axis('Down', 'Up', 1)
        # ~ bind_axis('Next', 'Prior', 2)
        # ~ bind_axis('KP_Left', 'KP_Right', 0)
        # ~ bind_axis('KP_Down', 'KP_Up', 1)
        # ~ bind_axis('KP_Next', 'KP_Prior', 2)
# ~ #        bind_axis('KP_4', 'KP_6', 0)
# ~ #        bind_axis('KP_2', 'KP_8', 1)
# ~ #        bind_axis('KP_3', 'KP_9', 2)
        # ~ bind_axis('bracketleft', 'bracketright', 3)
        # ~ bind_axis('semicolon', 'quoteright', 4)
        # ~ root_window.bind('<Key-KP_Add>', lambda event: commands.key_pressed('jogfast'))
        # ~ root_window.bind('<KeyRelease-KP_Add>', lambda event: commands.key_released('jogfast'))
        # ~ root_window.bind('<Key-KP_Subtract>', lambda event: commands.key_pressed('jogslow'))
        # ~ root_window.bind('<KeyRelease-KP_Subtract>', lambda event: commands.key_released('jogslow'))
        # ~ # sliders
        # ~ root_window.bind('`', lambda event: activate_ja_widget_or_set_feedrate(0))
        # ~ root_window.bind('<Control-Key-quoteleft>',lambda event: set_rapidrate(0))
        # ~ root_window.bind('<Alt-Key-quoteleft>',lambda event: set_jog_slider(0.0))
        # ~ for n in range(10):
            # ~ rate = n if n > 0 else 10
            # ~ root_window.bind('{}'.format(n), make_lambda(activate_ja_widget_or_set_feedrate, rate))
            # ~ root_window.bind('<Control-Key-{}>'.format(n), make_lambda(set_rapidrate, rate * 10))
            # ~ root_window.bind('<Alt-Key-{}>'.format(n), make_lambda(set_jog_slider, rate * 0.1))
        # ~ # homing
        # ~ if homing_order_defined:
            # ~ root_window.bind('<Control-Home>', commands.home_all_joints)
        # ~ else:
            # ~ root_window.bind('<Control-Home>', commands.home_joint)
        # ~ root_window.bind('<Shift-Home>', commands.home_joint)
        # ~ # touchoff
        # ~ root_window.bind('<Key-End>', lambda event: commands.touch_off_xy('1', 0, 0))
        # ~ root_window.bind('<KeyRelease-End>', lambda event: commands.touch_off_xy('0', 0, 0))
        # ~ root_window.bind('<Control-End>', commands.set_axis_offset)
        # ~ root_window.bind('<Shift-End>', commands.touch_off_system)
        # ~ root_window.bind('<Key-Delete>', lambda event: commands.key_pressed('Delete'))
        # ~ root_window.bind('<KeyRelease-Delete>', lambda event: commands.key_released('Delete'))
        # ~ # program
        # ~ root_window.bind('o', commands.open_file)
        # ~ root_window.bind('r', commands.task_run)
        # ~ root_window.bind('p', commands.task_pause)
        # ~ root_window.bind('<space>', commands.task_pause)
        # ~ root_window.bind('s', commands.task_resume)
        # ~ root_window.bind('<Control-r>', commands.reload_file)
        # ~ root_window.bind('<Control-s>', commands.save_gcode)
        # ~ # clearing 
        # ~ root_window.bind('<Control-k>', commands.clear_live_plot)
        # ~ root_window.bind('<Control-space>', lambda event: notifications.clear())
        # ~ # whacky keypad stuff
        # ~ keys1 = ['Decimal', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        # ~ # Down, Next, Left, Right, Up, and Prior are done in the jogging section above
        # ~ keys2 = ['Delete', 'Insert', 'End', None, None, None, 'Begin', None, 'Home', None, None]
        # ~ for k in range(len(keys2)):
            # ~ if keys2[k]:
                # ~ root_window.bind('<Key-KP_{}>'.format(keys2[k]), make_lambda(key_pressed, keys2[k]))
                # ~ root_window.bind('<KeyRelease-KP_{}>'.format(keys2[k]), make_lambda(key_released, keys2[k]))
                # ~ root_window.bind('<Mod2-KeyRelease-KP_{}>'.format(keys2[k]), make_lambda(key_released, keys1[k]))
        # ~ # add kb shortcuts to help menu
        # ~ rC('.menu.help','add','separator')
        # ~ rC('.menu.help','add','command','-command','wm transient .keys .;wm deiconify .keys; focus .keys.ok')
        # ~ rC('setup_menu_accel','.menu.help','end',_('Keyboard Shortcuts'))
        # ~ rC('.menu.help','add','command','-command','wm transient .keyp .;wm deiconify .keyp; focus .keyp.ok')
        # ~ rC('setup_menu_accel','.menu.help','end',_('Keypad Shortcuts'))
    # ~ # use arrow buttons for spinboxes
    # ~ for item in rpList + cpList:
        # ~ widget = item[0] + '.' + item[1]
        # ~ rC('bind',widget,'<Up>','%W invoke buttonup')
        # ~ rC('bind',widget,'<Down>','%W invoke buttondown')
        # ~ rC('bind',widget,'<Left>','::tk::EntrySetCursor %W [expr {[%W index insert] - 1}]')
        # ~ rC('bind',widget,'<Right>','::tk::EntrySetCursor %W [expr {[%W index insert] + 1}]')
