python 2.7.12 on Linux behaving differently than on Windows

BartC bc at freeuk.com
Thu Dec 8 07:43:18 EST 2016


On 08/12/2016 07:24, Gregory Ewing wrote:
> BartC wrote:

>> (I'm in the middle of porting my console editor to Linux. But one
>> problem is that on one Linux, half the key combinations (eg.
>> Shift+Ctrl+B) are not recognised.
>
> If you're reading characters from a tty device in raw
> mode (which I assume is what you mean by "console editor")
> I'm not aware of *any* Unix system that will let you
> distinguish between Ctrl+B and Shift+Ctrl+B that way.
> That's because the tty driver delivers ASCII characters,
> and there are no separate ASCII codes for shifted control
> characters.

Run the code below and start pressing keys. On both of my Linuxes, I get 
escape sequences shown when I Insert, Delete, Home, End, Page Up, Page 
Down, Up, Down, Left, Right and most of the function keys; not just 
single ASCII codes.

But I also get different sequences, on Ubuntu, when I press Shift, Ctrl 
or Alt with those keys, but not all shifts nor combinations will work 
(some have special meanings anyway).

Then I try the same on Debian (I think it is) on a Raspberry Pi, and 
most Shift and Ctrl are ignored, except for Ctrl A to Z (with a few gaps).

(Neither will see Shift+Ctrl+B, which means go to start of the file, 
same as Ctrl+Home. Ubuntu sees Ctrl+Home, but not Debian, although it 
will report Alt+Home. And some laptop keyboards already have Home on an 
Alternate-Function shift! It's a mess.)

>> Except that was only two Linuxes; perhaps on others, the keyboard will
>> likely be crippled in some other way.
>
> No, they'll all be the same -- if it has an ASCII code,
> you'll be able to get it from a tty device, otherwise you
> won't.
>

>> How people manage to do anything on such an OS I've no idea.
>
> Programs that need to be able to distinguish all of the
> modifiers are normally implemented as GUI applications,
> which get keyboard input a different way.

How do they work; what magic do they use to get that key information, 
and why can't it be done outside of a GUI? As I understand a Linux GUI 
is built on top of Linux.

----------------------
# Python 2 because of the 'print' handling

def getch():     # adapted from first getch I saw on the internet
         import sys, tty, termios
         fd = sys.stdin.fileno()
         old_settings = termios.tcgetattr(fd)
         tty.setraw(sys.stdin.fileno())
         ch = sys.stdin.read(1)
         termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
         return ord(ch)

print "Press keys"
print ("Hit Escape twice to quit")

escape=0
while 1:
     k=getch()

     if k==27:
         print
         print "Esc ",
         if escape: break
     elif k==13:
         print "Enter"
     elif k==10:
         print "Newline"
     elif k<=26:
         print "Ctrl",chr(k+64)
     else:
         print chr(k),

     escape = k==27

----------------------

(On another test, using a C-based getch(), pressing Enter returns code 
10 not 13; another difference to note. Either code appears to clash with 
Ctrl-M or Ctrl-J, a difference from Windows where Ctrl-J, Ctrl-M and 
Enter are distinct keys, as they are in actuality.)

-- 
Bartc



More information about the Python-list mailing list