Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use delete key or backspace #74

Closed
jhilker98 opened this issue Jan 3, 2021 · 19 comments
Closed

Unable to use delete key or backspace #74

jhilker98 opened this issue Jan 3, 2021 · 19 comments
Labels
Bug Something isn't working Core Add for issues having to do with core functions Inputs Issue with regards to keyboard and mouse
Milestone

Comments

@jhilker98
Copy link

jhilker98 commented Jan 3, 2021

Describe the bug
Hitting the backspace or delete keys appends '^?' to the text I enter instead of deleting it.

To Reproduce
Steps to reproduce the behavior:

  1. Write some text in any widget
  2. Press delete or backspace

Expected behavior
If I hit delete, the character the cursor is on should be deleted, but if I hit backspace, the previous character should be deleted.

Screenshots
Will update tomorrow with screenshots

Environment:

  • OS: Ubuntu 18.04
  • Terminal: Alacritty
  • Version: 0.1.2
@jhilker98 jhilker98 added the Bug Something isn't working label Jan 3, 2021
@jwlodek
Copy link
Owner

jwlodek commented Jan 4, 2021

I will try and reproduce this tomorrow. Do you have this bug only in Alacritty, or in other terminal emulators on your system as well? There have been some inconsistencies in behavior of py_cui in different terminals, though mostly on windows, I haven't seen something like this on linux.

@jwlodek jwlodek added Core Add for issues having to do with core functions Inputs Issue with regards to keyboard and mouse labels Jan 4, 2021
@jhilker98
Copy link
Author

I just tested in Gnome Terminal and had the same issue.

@jwlodek
Copy link
Owner

jwlodek commented Jan 4, 2021

Which widget/popup specifically is seeing this behavior?

@jhilker98
Copy link
Author

jhilker98 commented Jan 5, 2021 via email

@jwlodek jwlodek added this to the v0.2.0 milestone Feb 6, 2021
@JeremyBYU
Copy link

JeremyBYU commented Apr 15, 2021

I have the same exact issue as well. Ubuntu 18.04 gnome terminal.

EDIT:

It seems that the backspace key is not being set right from the ncurses library. My backspace is giving '127' vs ncurses showing '263'. I had to change this code in keys.py in py-cui:

# Pressing backspace returns 8 on windows?
if platform == 'win32':
    KEY_BACKSPACE   = 8
# Adds support for 'delete/backspace' key on OSX
elif platform == 'darwin':
    KEY_BACKSPACE   = 127
else:
    KEY_BACKSPACE   = 127 # curses.KEY_BACKSPACE # CHANGE HERE

@ghost
Copy link

ghost commented Apr 16, 2021

Same issue although Jeremy's solution fixes it.

@jwlodek
Copy link
Owner

jwlodek commented Apr 16, 2021

This is odd, because I tried to reproduce the issue on my Ubuntu 18.04 machine with gnome-terminal, but I couldn't get that kind of behavior. I wonder what this could possibly be caused by?

@ghost
Copy link

ghost commented Apr 16, 2021

I was using alacritty. Maybe alacritty uses different terminfo.

@jwlodek
Copy link
Owner

jwlodek commented Apr 16, 2021

I haven't tried alacritty, but @jhilker1 mentioned he tried gnome-terminal and saw the same behavior. It seems that the curses pre-defined backspace key code is not robust, we may need to consider some logic for checking if a key is one of the several possible backspace codes

@JeremyBYU
Copy link

Yeah, I think you may need to check for a few possible values:

https://stackoverflow.com/a/54303430

@lenormf
Copy link

lenormf commented May 24, 2021

I could reproduce the issue on Kitty.

@voryzen
Copy link
Contributor

voryzen commented Oct 5, 2021

+1 this issue, using konsole, zsh

@jwlodek
Copy link
Owner

jwlodek commented Oct 7, 2021

@voryzen could you run the following curses program on your system and let me know what value of keypress you get when pressing backspace/delete?

import sys,os
import curses

def draw_menu(stdscr):
    k = 0
    cursor_x = 0
    cursor_y = 0

    # Clear and refresh the screen for a blank canvas
    stdscr.clear()
    stdscr.refresh()

    # Start colors in curses
    curses.start_color()
    curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)

    # Loop where k is the last character pressed
    while (k != ord('q')):

        # Initialization
        stdscr.clear()
        height, width = stdscr.getmaxyx()

        if k == curses.KEY_DOWN:
            cursor_y = cursor_y + 1
        elif k == curses.KEY_UP:
            cursor_y = cursor_y - 1
        elif k == curses.KEY_RIGHT:
            cursor_x = cursor_x + 1
        elif k == curses.KEY_LEFT:
            cursor_x = cursor_x - 1

        cursor_x = max(0, cursor_x)
        cursor_x = min(width-1, cursor_x)

        cursor_y = max(0, cursor_y)
        cursor_y = min(height-1, cursor_y)

        # Declaration of strings
        title = "Curses example"[:width-1]
        subtitle = "Written by Clay McLeod"[:width-1]
        keystr = "Last key pressed: {}".format(k)[:width-1]
        statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)
        if k == 0:
            keystr = "No key press detected..."[:width-1]

        # Centering calculations
        start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
        start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
        start_x_keystr = int((width // 2) - (len(keystr) // 2) - len(keystr) % 2)
        start_y = int((height // 2) - 2)

        # Rendering some text
        whstr = "Width: {}, Height: {}".format(width, height)
        stdscr.addstr(0, 0, whstr, curses.color_pair(1))

        # Render status bar
        stdscr.attron(curses.color_pair(3))
        stdscr.addstr(height-1, 0, statusbarstr)
        stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
        stdscr.attroff(curses.color_pair(3))

        # Turning on attributes for title
        stdscr.attron(curses.color_pair(2))
        stdscr.attron(curses.A_BOLD)

        # Rendering title
        stdscr.addstr(start_y, start_x_title, title)

        # Turning off attributes for title
        stdscr.attroff(curses.color_pair(2))
        stdscr.attroff(curses.A_BOLD)

        # Print rest of text
        stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
        stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
        stdscr.addstr(start_y + 5, start_x_keystr, keystr)
        stdscr.move(cursor_y, cursor_x)

        # Refresh the screen
        stdscr.refresh()

        # Wait for next input
        k = stdscr.getch()

def main():
    curses.wrapper(draw_menu)

if __name__ == "__main__":
    main()

@voryzen
Copy link
Contributor

voryzen commented Oct 10, 2021

Hi @jwlodek,

127

I used both konsole and gnome-terminal

@voryzen
Copy link
Contributor

voryzen commented Oct 18, 2021

@jwlodek if it helps at all, these are the changes that I've made to my py_cui keys.py

# Pressing backspace returns 8 on windows?
if platform == 'win32':
    KEY_BACKSPACE   = 8
# Adds support for 'delete/backspace' key on OSX/linux
elif platform == 'darwin' or platform == 'linux':
    KEY_BACKSPACE   = 127
else:
    KEY_BACKSPACE   = curses.KEY_BACKSPACE

@PabloLec
Copy link
Contributor

PabloLec commented Oct 24, 2021

This issue has been reported on RecoverPy as you can see with the link above.

A simple condition with platform as in:

elif platform == 'darwin' or platform == 'linux':
    KEY_BACKSPACE   = 127

Won't to the job. I've always run PyCui apps on Linux and never had this issue.

For reference, when running @jwlodek testing script, I get 263 for backspace.

OS: Kubuntu 21.04 x86_64
Shell: zsh 5.8
Terminal: terminator

This is most likely caused by xterm. I guess we should catch all three values for backspace.

@PabloLec
Copy link
Contributor

PabloLec commented Oct 24, 2021

I just published a PR trying to resolve this.
It would be great if someone encountering the initial issue could try it.

  • git clone https://github.com/PabloLec/py_cui.git (clone my fork)
  • git checkout fix-backspace (go to branch fix-backspace)
  • Make sure to be in a virtual env or just uninstall py_cui
  • pip install -e . (Install py_cui from branch fix-backspace)
  • And try an example, e.g python examples/simple_todo_list.py

@voryzen
Copy link
Contributor

voryzen commented Nov 28, 2021

HI @PabloLec,
After installing your backspace fix.

  File "/home/voryzen/Downloads/py_cui/py_cui/widgets.py", line 102, in add_key_command
    self._keybindings[key] = command
AttributeError: 'ScrollMenu' object has no attribute '_keybindings'

@PabloLec
Copy link
Contributor

Hi!

Yes this is fixed by #159 .

@PabloLec PabloLec mentioned this issue Dec 4, 2021
3 tasks
@jwlodek jwlodek closed this as completed Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Core Add for issues having to do with core functions Inputs Issue with regards to keyboard and mouse
Projects
None yet
Development

No branches or pull requests

6 participants