0

I want to be able to navigate through my menu with the arrows and I don't know how to apply it on my program.

I have a simple menu

private static String[] options = {"1- Plus 1",
            "2- Minus 1",
            "3- Exit",
    };
        
        public void printMenu(){
            for (String option : options){
                System.out.println(option);
            }
            System.out.print("Choose an option : ");
        }
        
        public void main(String[] args) {
            
            int option = 1;
            int a = 0;
            Scanner scan = new Scanner(System.in);
           
            while (option != options.length=){
                printMenu();
                try {
                    option = scan.nextInt();
                    switch (option){
                        case 1: a++; break;
                        case 2: a--; break;
                        case 3: exit(0);
                    }
                }
                catch (Exception ex){
                    System.out.println("Input a number between 1 and  " + options.length);
                    scan.next();
                }
            }
        }

and have a Class for the JNativeHook with the keys i want to use (UP, DOWN and ENTER) and I want to be able to move through the options up and down and use the Enter to select the one I want. The problem is that I don't have any idea how to do it, is there any way to do it? PS: Here is my JNativeHook class:


public class GlobalKeyListenerExample implements NativeKeyListener {
    
    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
        
        switch(e.getKeyCode()) {
        case NativeKeyEvent.VC_UP:
            try {
                GlobalScreen.registerNativeHook();
            }
            catch (NativeHookException ex) {
                System.err.println("There was a problem registering the native hook.");
                System.err.println(ex.getMessage());
            
                System.exit(1);
            }
        case NativeKeyEvent.VC_DOWN:
            try {
                GlobalScreen.registerNativeHook();
            }
            catch (NativeHookException ex) {
                System.err.println("There was a problem registering the native hook.");
                System.err.println(ex.getMessage());
            
                System.exit(1);
            }
        case NativeKeyEvent.VC_ENTER:
            try {
                GlobalScreen.registerNativeHook();
            }
            catch (NativeHookException ex) {
                System.err.println("There was a problem registering the native hook.");
                System.err.println(ex.getMessage());
            
                System.exit(1);
            }
        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

}
Ifini
  • 1
  • 1

0 Answers0