Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #90732
| Newsgroups | comp.lang.c++ |
|---|---|
| Date | 2023-06-26 02:06 -0700 |
| References | <u79ug0$i1ks$1@dont-email.me> |
| Message-ID | <e992ebdc-2d53-4134-9cfd-fd9339c8ffe5n@googlegroups.com> (permalink) |
| Subject | Re: Parsing multi-byte keystrokes |
| From | wij <wyniijj5@gmail.com> |
On Monday, June 26, 2023 at 1:45:54 AM UTC+8, R.Wieser wrote:
> Hello all,
>
> I've set the console input to raw so I can see keys coming in when they are
> pressed.
>
> For that I've been writing a big "switch ... case" to parse the multi-byte
> keystrokes into "records" that than can be easily worked with or discarded.
>
> As this parsing needs to be done by pretty-much every console-based program
> which wants to do any kind of interactive stuff I was wondering if this
> perhaps is already part of Linux ...
>
> If it is (and thats still a big "if"), where can I find it ?
>
> Second question : How do I check for modifier keys (like alt, shift, ctrl)
> on the pressed keys or seperatily ? I would like to be able to, as an
> example, catch a ctrl-cursor-left as well as a normal or a shift-curor-left.
>
> Regards,
> Rudy Wieser
/* Copyright is licensed by GNU LGPL, see file COPYING. by I.J.Wang 2005
Raw mode TTY for keyboard input.
Build: g++ t_kb.cpp -lwy
*/
#include <Wy.stdio.h> // for cin,cout,cerr
#include <Wy.termios.h>
#include <Wy.unistd.h>
using namespace Wy;
// TTY i/o (raw mode terminal)
// RawTerm changes tty attributes so that we can read key stroke without press ENTER
//
// Warning: Many virtual members of ChrFile are not reimplemented for simplicity.
//
class RawTerm : public ChrFile {
Termios m_ptio; // Previous(original) termios setting
public:
WY_DECL_REPLY;
RawTerm() {};
RawTerm(const char*, int);
~RawTerm();
Errno getch(char&);
Errno write(const char);
Errno write(const char*);
} static mtty("/dev/tty",O_RDWR);
RawTerm::RawTerm(const char* pathname, int flag)
: ChrFile(pathname,flag)
{
Errno r=tcflush(*this,TCIFLUSH);
if(r!=Ok) {
WY_THROW( Reply(r) );
}
if((r=tcgetattr(*this,m_ptio))!=Ok) {
WY_THROW( Reply(r) );
}
try {
// Set the terminal mode
//
Termios ntio(m_ptio);
ntio.iflag_and(~(BRKINT| ICRNL| IXON| INPCK | ISTRIP));
ntio.lflag_and(~(ICANON| ECHO| ISIG| TOSTOP| IEXTEN));
ntio.oflag_and( ~(OPOST) );
ntio.cflag_or( CS8 );
ntio.cc_VMIN(1);
ntio.cc_VTIME(0);
if((r=tcsetattr(*this,TCSANOW,ntio))!=Ok) {
WY_THROW( Reply(r) );
}
// Read back check
Termios tmptio;
if((r=tcgetattr(*this,tmptio))!=Ok) {
WY_THROW( Reply(r) );
}
// Omitted: Check termios setting accepted or not
}
catch(const Termios::Reply& e) {
WY_THROW( Reply(r) );
};
};
RawTerm::~RawTerm()
{
// If *this is default, these functions just fail (no effect for any device)
Errno r;
r=tcflush(*this,TCIFLUSH);
r=tcsetattr(*this,TCSANOW,m_ptio);
};
/ Get one char from terminal
//
Errno RawTerm::getch(char& ch)
{
Errno r;
size_t n;
if((r=ChrFile::read(&ch,sizeof(ch),n))!=Ok) {
WY_RETURN(r);
}
if(n==0) {
WY_RETURN(EIO);
}
return r;
};
Errno RawTerm::write(const char ch)
{
Errno r;
size_t n;
if((r=ChrFile::write(&ch,sizeof(ch),n))!=Ok) {
WY_RETURN(r);
}
if(n!=1) {
WY_RETURN(EIO);
}
return r;
};
Errno RawTerm::write(const char* str)
{
if(str==NULL) {
WY_RETURN(EFAULT);
}
const size_t slen=Wy::strlen(str);
Errno r;
size_t n;
if((r=ChrFile::write(str,slen,n))!=Ok) {
WY_RETURN(r);
}
if(n!=slen) {
WY_RETURN(EIO);
}
return r;
};
int main(int, char* [])
try {
Errno r;
if((r=mtty.write("中文"))!=Ok) {
WY_THROW(r);
}
for(;;) {
char ch;
if((r=mtty.getch(ch))!=Ok) {
WY_THROW(r);
}
if((ch=='q')||(ch=='Q')) {
break;
}
/* if((r=mtty.write(ch))!=Ok) {
WY_THROW(r);
}*/
cout << (unsigned char)ch << ',';
}
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "main caught(...)" WY_ENDL;
throw;
};
----------------------------------
[]$ ./a.out
中文27,91,51,126,27,91,51,59,50,126,27,91,51,59,53,126,27,91,51,59,51,126, // keystroke: del, sh-del, ctrl-del, alt-del
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-25 19:45 +0200
Re: Parsing multi-byte keystrokes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-06-25 20:34 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-25 21:39 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-25 15:06 -0700
Re: Parsing multi-byte keystrokes Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-06-25 20:18 -0400
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-25 19:10 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 08:46 +0200
Re: Parsing multi-byte keystrokes Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-06-25 18:39 -0400
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-26 00:53 +0100
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 09:11 +0200
Re: Parsing multi-byte keystrokes Christian Gollwitzer <auriocus@gmx.de> - 2023-06-26 09:32 +0200
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-26 13:50 +0100
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 15:03 +0200
Re: Parsing multi-byte keystrokes Christian Gollwitzer <auriocus@gmx.de> - 2023-06-26 22:27 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 22:48 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 14:35 -0700
Re: Parsing multi-byte keystrokes Christian Gollwitzer <auriocus@gmx.de> - 2023-06-26 23:58 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 16:26 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 08:50 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 01:39 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 13:43 +0200
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-06-26 15:26 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 18:04 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 12:01 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 21:59 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 14:15 -0700
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 22:35 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-29 13:15 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 14:03 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 17:49 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 12:23 -0700
Re: Parsing multi-byte keystrokes red floyd <no.spam.here@its.invalid> - 2023-06-28 14:35 -0700
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-06-29 00:17 +0000
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-29 00:43 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-29 14:04 +0200
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-06-29 15:44 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-29 18:37 +0200
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-30 14:14 +0100
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-30 18:12 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-30 20:32 +0200
Re: Parsing multi-byte keystrokes Paavo Helde <eesnimi@osa.pri.ee> - 2023-06-30 22:57 +0300
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-01 09:49 +0200
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-30 21:36 +0100
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-30 13:45 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-01 10:33 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-01 11:58 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-02 11:03 +0200
Re: Parsing multi-byte keystrokes Christian Gollwitzer <auriocus@gmx.de> - 2023-07-02 23:15 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 09:12 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-02 15:39 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 10:12 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-03 13:28 +0000
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-03 12:43 -0400
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-07-03 15:41 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 21:32 +0200
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-03 21:06 +0000
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-04 05:20 -0400
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-04 13:56 +0000
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-04 16:03 -0400
Re: Parsing multi-byte keystrokes kalevi@kolttonen.fi (Kalevi Kolttonen) - 2023-07-04 21:03 +0000
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-04 14:33 -0700
Re: Parsing multi-byte keystrokes kalevi@kolttonen.fi (Kalevi Kolttonen) - 2023-07-04 21:55 +0000
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-05 13:58 +0000
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-05 13:56 +0000
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-07-04 15:54 +0000
Re: Parsing multi-byte keystrokes David Brown <david.brown@hesbynett.no> - 2023-07-04 18:03 +0200
Re: Parsing multi-byte keystrokes kalevi@kolttonen.fi (Kalevi Kolttonen) - 2023-07-04 16:33 +0000
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-04 16:41 +0000
Re: Parsing multi-byte keystrokes kalevi@kolttonen.fi (Kalevi Kolttonen) - 2023-07-04 16:52 +0000
Re: Parsing multi-byte keystrokes David Brown <david.brown@hesbynett.no> - 2023-07-05 09:50 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-05 14:00 +0000
Re: Parsing multi-byte keystrokes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-07-05 11:21 -0700
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-05 20:24 +0000
Re: Parsing multi-byte keystrokes kalevi@kolttonen.fi (Kalevi Kolttonen) - 2023-07-05 19:31 +0000
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-07-04 19:05 +0000
Re: Parsing multi-byte keystrokes red floyd <no.spam.here@its.invalid> - 2023-07-05 18:31 -0700
Cars (Was: Parsing what is Linux?) gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-06 08:40 +0000
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-07-06 08:55 +0000
Cars (Was: Parsing multi-byte keystrokes) gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-06 09:27 +0000
Re: Cars (Was: Parsing multi-byte keystrokes) Muttley@dastardlyhq.com - 2023-07-06 15:43 +0000
Totally insane (Was: Cars (Was: Parsing multi-byte keystrokes)) gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-06 16:53 +0000
Re: Totally insane (Was: Cars (Was: Parsing multi-byte keystrokes)) Muttley@dastardlyhq.com - 2023-07-07 07:15 +0000
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-06-30 17:00 -0400
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-01 07:53 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-01 10:30 +0200
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-02 04:04 -0400
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-02 11:27 +0200
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-02 14:51 -0400
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-02 22:49 +0200
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-02 18:56 -0400
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 10:59 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-07-03 13:34 +0000
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-03 12:20 -0400
Re: Parsing multi-byte keystrokes Richard Harnden <richard.nospam@gmail.com> - 2023-07-03 18:02 +0100
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-03 14:40 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 22:50 +0200
Re: Parsing multi-byte keystrokes James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-04 05:47 -0400
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-03 14:36 -0700
Re: Parsing multi-byte keystrokes David Brown <david.brown@hesbynett.no> - 2023-07-03 11:38 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 13:04 +0200
Re: Parsing multi-byte keystrokes David Brown <david.brown@hesbynett.no> - 2023-07-03 15:11 +0200
Re: Parsing multi-byte keystrokes Christian Gollwitzer <auriocus@gmx.de> - 2023-07-03 20:35 +0200
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-07-03 19:16 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-03 22:55 +0200
Re: Parsing multi-byte keystrokes Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-07-01 03:03 -0700
Re: Parsing multi-byte keystrokes Muttley@dastardlyhq.com - 2023-07-01 10:30 +0000
Re: Parsing multi-byte keystrokes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-07-01 22:37 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-07-02 09:42 +0200
Re: Parsing multi-byte keystrokes Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-06-29 22:21 -0400
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-30 17:50 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-25 23:20 +0000
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-25 17:10 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 09:21 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 01:29 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 13:30 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 12:18 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 22:37 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 14:32 -0700
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 22:41 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-29 13:58 +0200
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-29 13:59 -0700
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 01:30 -0700
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 14:00 +0000
Re: Parsing multi-byte keystrokes "Fred. Zwarts" <F.Zwarts@HetNet.nl> - 2023-06-26 12:31 +0200
Re: Parsing multi-byte keystrokes "Fred. Zwarts" <F.Zwarts@HetNet.nl> - 2023-06-26 12:47 +0200
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 13:56 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 13:58 +0000
Re: Parsing multi-byte keystrokes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-26 12:08 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 08:56 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 14:02 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 17:46 +0200
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-26 18:19 +0100
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 18:36 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 21:24 +0200
Re: Parsing multi-byte keystrokes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-26 22:12 +0100
Re: Parsing multi-byte keystrokes wij <wyniijj5@gmail.com> - 2023-06-26 02:06 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 13:49 +0200
Re: Parsing multi-byte keystrokes scott@slp53.sl.home (Scott Lurndal) - 2023-06-26 14:05 +0000
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 17:58 +0200
Re: Parsing multi-byte keystrokes wij <wyniijj5@gmail.com> - 2023-06-27 13:32 -0700
Re: Parsing multi-byte keystrokes gazelle@shell.xmission.com (Kenny McCormack) - 2023-06-28 02:17 +0000
Re: Parsing multi-byte keystrokes wij <wyniijj5@gmail.com> - 2023-06-27 20:26 -0700
Re: Parsing multi-byte keystrokes "R.Wieser" <address@is.invalid> - 2023-06-26 19:24 +0200
csiph-web