0

I want to use curses.KEY_UP and curses.KEY_DOWN to increment and decrement the index using the functions I created. My handlers can use any key but it doesn't seem to be working when using the up and down key.

import npyscreen, curses


index = 0
class MyTestApp(npyscreen.NPSAppManaged):
    def onStart(self):
        self.registerForm("MAIN", MainForm())

class MainForm(npyscreen.Form):
    def create(self):

    

        for i in range(10):
            self.add(npyscreen.FixedText, value='message:'+str(i))
            
        
        self.add_handlers({"^R": self.spawn_notify_popup,
                            curses.KEY_UP: self.minus_one,
                            curses.KEY_DOWN: self.add_one})


    def add_one(self, key):
        global index
        if(index < 30):
            index+=1
        
    def minus_one(self, key):
        global index
        if(index > 0):
            index-=1   
          
            
    
    def spawn_notify_popup(self, code_of_key_pressed):
        message_to_display = 'I popped up passed: {}'.format(index)
        npyscreen.notify_confirm(message_to_display, title= 'title')
        
    
            
    def afterEditing(self):
        self.parentApp.setNextForm(None)

if __name__ == '__main__':
    TA = MyTestApp()
    TA.run()
Mark
  • 117
  • 2
  • 9

1 Answers1

0

This post is rather old. Like you, I could not get the code to run. I don't know whether the app as a whole responds to the handlers you are interested in.

A solution is to register the handler with each widget. However that hijacks the UP and DOWN keys, and you no longer can navigate the TUI. I slightly modified the example to respond to the LEFT and RIGHT keys. The example below will run (however, I don't know whether it helps you out at all)

import npyscreen
import curses

index = 15

class MainForm(npyscreen.Form):

    def create(self):

        self.add_handlers({"^R": self.spawn_notify_popup,})

        for i in range(10):
            h = self.add(npyscreen.FixedText, value='message:'+str(i))
            h.handlers.update({curses.KEY_LEFT: self.minus_one,
                               curses.KEY_RIGHT: self.add_one})

    def add_one(self, ch):
        global index
        if(index < 30):
            index+=1

    def minus_one(self, ch):
        global index
        if(index > 0):
            index-=1

    def spawn_notify_popup(self, code_of_key_pressed):
        global index
        message_to_display = 'Value: {}'.format(index)
        npyscreen.notify_confirm(message_to_display, title= 'title')

    def afterEditing(self):
        self.parentApp.setNextForm(None)


class MyTestApp(npyscreen.NPSAppManaged):
    def onStart(self):
        self.registerForm("MAIN", MainForm())


if __name__ == '__main__':
    TA = MyTestApp()
    TA.run()

You can also look at this example from the npyscreen github repo (curser.KEY_UP npyscreen example) to see how the author intended to use the KEY_UP and KEY_DOWN handlers

vpappano
  • 111
  • 1
  • 11