Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ getch() in perl?

Tags:

perl

getch

In c++, there is a functio, getch(), which returns the variable of the key you pressed - like enter would be 13. How could I do this in perl?

like image 255
Karandeep Singh Avatar asked Jun 07 '10 08:06

Karandeep Singh


2 Answers

You can use Term::ReadKey.

like image 186
Svante Avatar answered Sep 28 '22 07:09

Svante


in short:

$x = ord(getc);

detailed:

$c = getc(STDIN);
$x = ord($c);

from perldoc -f getc :

"However, it cannot be used by itself to fetch single 
characters without waiting for the user to hit enter. 
For that, try something more like":

   1.  if ($BSD_STYLE) {
   2. system "stty cbreak </dev/tty >/dev/tty 2>&1";
   3. }
   4. else {
   5. system "stty", '-icanon', 'eol', "\001";
   6. }
   7.
   8. $key = getc(STDIN);
   9.
  10. if ($BSD_STYLE) {
  11. system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  12. }
  13. else {
  14. system 'stty', 'icanon', 'eol', '^@'; # ASCII NUL
  15. }
  16. print "\n";
like image 43
Oleg Razgulyaev Avatar answered Sep 28 '22 09:09

Oleg Razgulyaev