-1

I know how to read keyboard in Linux without stopping (like kbhit() behavior under Windows). So, whenever it returns, I know there is a key-press activity.

Then I'll try to read the key via getch() function.

My question is that after a hit on keyboard, how I could know that the Shift+Insert key combination (paste shortcut as Ctrl+V does in Windows) is pressed?

Paco
  • 4,520
  • 3
  • 29
  • 53
  • This is completely depending on your actual GUI system. There's no globally portable way to grab the copy buffers. – πάντα ῥεῖ Apr 25 '15 at 11:08
  • I'm writing a program under Linux/C to wait for user commands (at a prompt I prepared), and response it. I want to handle UP/DOWN keys to make history of commands available. So I'm not using fgets() (It doesn't aware me of pressing UP/DOWN arrow keys by user). I've done this way: waiting for a key, reading it with getch() and inserting it in a string in my app, Till ENTER pressed. When user pastes some string in prompt, getch() call gives the first letter in pasted string. If I loop calling getch() it gives the whole pasted-string, but don't know when I have to finish the loop. – Moh K. Moghadam Apr 25 '15 at 11:45
  • Repeating - there is no *globally portable way*. A paste need not end with a newline. For the given scenario, you can make your program wait a short amount of time and then stop reading. – Thomas Dickey Apr 25 '15 at 11:58
  • Thank you Thomas. I will try it and write about my experience. – Moh K. Moghadam Apr 25 '15 at 12:11

1 Answers1

0

As noted, there is no globally portable way to accomplish this because:

  • aside from the special case of a Linux-specific program which uses special calls to detect key-presses, your program would only see the pasted text — no clues to how it got there.
  • there likewise would be no clue to when the paste ended (pastes need not end with a newline, they can be words, blanks, etc).

Instead, a workable solution would be to use POSIX termios to make your program's input stream work in raw mode, and set a timeout for reads. Then, the program could read characters as long as they came "quickly enough" (your decision for how quickly).

These related questions show some examples of POSIX termios, including the use of timeouts:

Alternatively, there is the select call:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • After your suggestion to wait some amount of time and break the loop of getch(), I was thinking about how to interrupt getch() call, when the supposed time spent. But then I realized that the problem would be fixed up if I remove "if(KeyboardHit()) /* behaves like kbhit() in Windows */ return 0;" from that loop. I did it and now, when some string pasted, the getch() loop automatically continues till all characters (in pasted string) got into the program. Thank you very much. – Moh K. Moghadam Apr 28 '15 at 06:26