Qt-interest Archive, May 2008
keyboard input in console application
Message 1 in thread
Hi
Does anybody know right way to grab keyboard input in console application ?
I have standard console application implementing some tcp server. I want to
have way to stop it without Ctrl+C pressing. Also i want to implement some
shortcut commands. Is it possible ?
--
[ signature omitted ]
Message 2 in thread
Hello! From your question I think you're running a QCoreApplication on main
thread with some network operations. The event loop you're using doesn't have
functions for getting input from the console. I assume you're using linux
here.
For reading keystrokes you can use external libraries (like ncurses) or simple
C functions like "fscanf( stdin, ... )" or "fread( ..., stdin )", both
defined in <stdio.h>.
However, if you choose a blocking call (like fscanf(..) or ncurses's getch()),
you have to do it in another thread, to avoid blocking the tcp server you are
running on the main thread.
Thus you can create a ConsoleReader class that inherits QThread, do the input
acquisition and processing in the run() function and emit control signals.
You will connect those signals to some QObjects on the main thread (to
implement the functionality you need).
Regards,
Enrico Ros
On Saturday 17 May 2008 12:57:24 nntp.trolltech.com wrote:
> Hi
> Does anybody know right way to grab keyboard input in console application ?
> I have standard console application implementing some tcp server. I want to
> have way to stop it without Ctrl+C pressing. Also i want to implement some
> shortcut commands. Is it possible ?
--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f
Sponsor:
Scopri le tue passioni con Leonardo.it!
*
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=7614&d=17-5
--
[ signature omitted ]
Message 3 in thread
"Enrico Ros" <enrico.qt@xxxxxxxx> сообщил/сообщила в новостях следующее:
news:200805171435.33693.enrico.qt@xxxxxxxxxxx
> Hello! From your question I think you're running a QCoreApplication on
> main
> thread with some network operations. The event loop you're using doesn't
> have
> functions for getting input from the console. I assume you're using linux
> here.
>
> For reading keystrokes you can use external libraries (like ncurses) or
> simple
> C functions like "fscanf( stdin, ... )" or "fread( ..., stdin )", both
> defined in <stdio.h>.
> However, if you choose a blocking call (like fscanf(..) or ncurses's
> getch()),
> you have to do it in another thread, to avoid blocking the tcp server you
> are
> running on the main thread.
>
> Thus you can create a ConsoleReader class that inherits QThread, do the
> input
> acquisition and processing in the run() function and emit control signals.
> You will connect those signals to some QObjects on the main thread (to
> implement the functionality you need).
>
> Regards,
> Enrico Ros
>
> On Saturday 17 May 2008 12:57:24 nntp.trolltech.com wrote:
>> Hi
>> Does anybody know right way to grab keyboard input in console application
>> ?
>> I have standard console application implementing some tcp server. I want
>> to
>> have way to stop it without Ctrl+C pressing. Also i want to implement
>> some
>> shortcut commands. Is it possible ?
>
>
>
>
> --
> Email.it, the professional e-mail, gratis per te: http://www.email.it/f
>
> Sponsor:
> Scopri le tue passioni con Leonardo.it!
> *
> Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=7614&d=17-5
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
Thanks.
Yes, i thought same, but i'm wondered qt doesn't have console classes to do
such things. Really i want make this application to work on any platform and
using external libraries is not best way in this case.
--
[ signature omitted ]
Message 4 in thread
On Saturday 17 May 2008 14:45:57 Sergey Kondryukov wrote:
> Thanks.
> Yes, i thought same, but i'm wondered qt doesn't have console classes to do
> such things. Really i want make this application to work on any platform
> and using external libraries is not best way in this case.
I found out that there is no need for external libraries:
Just use QTextStream, like this:
QTextStream qin(stdin, QFile::ReadOnly);
forever {
QString line = qin.readLine();
if (!line.isNull()) {
// .. process input
}
}
You still have to put this in a thread, but at least you are multi-platform
now ;-)
Regards,
Enrico Ros
--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f
Sponsor:
Carta Eureka realizza i tuoi sogni! Fido fino a 3.000 eruo, rate a partire da 20 euro e canone gratis il 1° anno. Scoprila!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=7876&d=18-5
--
[ signature omitted ]
Message 5 in thread
"Enrico Ros" <enrico.qt@xxxxxxxx> сообщил/сообщила в новостях следующее:
news:200805181919.35093.enrico.qt@xxxxxxxxxxx
> On Saturday 17 May 2008 14:45:57 Sergey Kondryukov wrote:
>> Thanks.
>> Yes, i thought same, but i'm wondered qt doesn't have console classes to
>> do
>> such things. Really i want make this application to work on any platform
>> and using external libraries is not best way in this case.
>
> I found out that there is no need for external libraries:
> Just use QTextStream, like this:
>
> QTextStream qin(stdin, QFile::ReadOnly);
> forever {
> QString line = qin.readLine();
> if (!line.isNull()) {
> // .. process input
> }
> }
>
> You still have to put this in a thread, but at least you are
> multi-platform
> now ;-)
>
Wow ! That is cool ! Many thanks !
Yet another question. Can i use qin.device.readyRead signal here ?
Does it work on windows ?
--
[ signature omitted ]
Message 6 in thread
On Monday 19 May 2008 09:49:30 Sergey Kondryukov wrote:
> > You still have to put this in a thread
> Wow ! That is cool ! Many thanks !
it's not.
> Yet another question. Can i use qin.device.readyRead signal here ?
way better idea! Well QxtStdio. or just do what it does yourself:
- disable stdin buffer
- wrap QFile around stdin
- use QSocketNotifier to get readyRead() from stdin
--
[ signature omitted ]
Message 7 in thread
"Arvid Ephraim Picciani" <aep@xxxxxxxxxxxxxxx> сообщил/сообщила в новостях
следующее: news:200805191125.34866.aep@xxxxxxxxxxxxxxxxxx
> On Monday 19 May 2008 09:49:30 Sergey Kondryukov wrote:
>> > You still have to put this in a thread
>> Wow ! That is cool ! Many thanks !
>
> it's not.
>
>> Yet another question. Can i use qin.device.readyRead signal here ?
>
> way better idea! Well QxtStdio. or just do what it does yourself:
> - disable stdin buffer
> - wrap QFile around stdin
> - use QSocketNotifier to get readyRead() from stdin
>
In windows select (and consequently QSocketNotifier) works with sockets
only.
Did you ever try QxtStdio on windows ?
--
[ signature omitted ]
Message 8 in thread
On Monday 19 May 2008 14:22:42 Sergey Kondryukov wrote:
> "Arvid Ephraim Picciani" <aep@xxxxxxxxxxxxxxx> сообщил/сообщила в новостях
> следующее: news:200805191125.34866.aep@xxxxxxxxxxxxxxxxxx
>
> > On Monday 19 May 2008 09:49:30 Sergey Kondryukov wrote:
> >> > You still have to put this in a thread
> >>
> >> Wow ! That is cool ! Many thanks !
> >
> > it's not.
> >
> >> Yet another question. Can i use qin.device.readyRead signal here ?
> >
> > way better idea! Well QxtStdio. or just do what it does yourself:
> > - disable stdin buffer
> > - wrap QFile around stdin
> > - use QSocketNotifier to get readyRead() from stdin
>
> In windows select (and consequently QSocketNotifier) works with sockets
> only.
obviously QSocketNotifier won't use select on windows.
> Did you ever try QxtStdio on windows ?
yup. works fine.
--
[ signature omitted ]
Message 9 in thread
>> In windows select (and consequently QSocketNotifier) works with sockets
>> only.
>
>obviously QSocketNotifier won't use select on windows.
It uses WSAAsyncSelect which works with sockets only like select.
See qeventdispatcher_win.cpp
>> Did you ever try QxtStdio on windows ?
>
>yup. works fine.
Strange, it doesn't work on my side. Honestly, i checked it.
And to be honestly, i can't understand how it can work if it uses
WSAAsyncSelect.
--
[ signature omitted ]
Message 10 in thread
On Monday 19 May 2008 16:48:26 Sergey Kondryukov wrote:
> Strange, it doesn't work on my side.
uh, then my unit tests are bad. will check manually. thanks for the hint
--
[ signature omitted ]
Message 11 in thread
On Saturday 17 May 2008 12:57:24 nntp.trolltech.com wrote:
> Hi
> Does anybody know right way to grab keyboard input in console application ?
> I have standard console application implementing some tcp server. I want to
> have way to stop it without Ctrl+C pressing. Also i want to implement some
> shortcut commands. Is it possible ?
http://docs.libqxt.org/classQxtStdio.html
--
[ signature omitted ]