0

I am trying to write a C program which is aware of the control / alt / shift key being pressed down. I found something that provides this functionality in Java, but that's not helping me too much.

void CMousepresentView::OnDraw(CDC* pDC)
{
int shiftValue=::GetKeyState(VK_SHIFT);
if(!shiftValue)
pDC->TextOut(0,50,"Shift not pressed");
else
pDC->TextOut(0,50,"Shift pressed");

int ctrlValue=::GetKeyState(VK_CONTROL);
if(!ctrlValue)
pDC->TextOut(0,100,"Ctrl not pressed");
else
pDC->TextOut(0,100,"Ctrl pressed");
}

So what I have so far is quite rudimentary but I must start somewhere. It doesn't work though, at all.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
  char ch;

  do {
    ch = getchar();
    putchar(ch);
  } while(iscntrl(ch));
  return 0;
}

I was hoping that iscntrl would at least give me some reaction from the system to start debugging and identifying the control sequence keypresses. No such luck.

If I could see an example that outputs "control is pressed / control is released", I could probably figure out the rest.

Update:
Have had some progress with this http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/

Update:

I think the answer is in using xlib. Thanks everyone.

Justin
  • 367
  • 1
  • 5
  • 15
  • Are you writing a program that works in a terminal window, or that creates its own window? If the former, Joachim is correct. If the latter, there is a way but I don't know how to do it. – zwol Nov 12 '11 at 05:27
  • Incidentally, the top fragment is Windows, and Windows has an easy enough way that I don't recall right now. – Joshua Nov 12 '11 at 05:30
  • Well I do see some response from the system while doing "cat /dev/input/by-path/platform-i8042-serio-0-event-kbd", so I think that it is possible somehow – Justin Nov 13 '11 at 22:21

2 Answers2

2

You can not check for the silent keys in a console program, not just those keys. If using something like ncurses you might get them as modifiers on other keys.

If you want to make a program with a graphical user interface, it's not a problem. Qt is a popular framework for that. Check the documentation for the framework you select.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Why wouldn't it work in the console but in Qt? That doesn't make sense. I'm wondering if I capture /dev/input/by-path/platform-i8042-serio-0-event-kbd I might be able to do it. – Justin Nov 13 '11 at 22:17
  • @Justin: it would work in the Linux console, but not on xterms. – ninjalj Nov 13 '11 at 23:13
  • @Justin QT is a framework for graphical user interfaces, and when a graphical program is in focus it can receive events for all keys. Text programs, like those in an xterm, can only receive textual input, sometimes with modifiers (like ALT and CTRL), but never ALT by itself as it doesn't generate a character. – Some programmer dude Nov 14 '11 at 06:20
0

You may find the results of this question relevant, by using a raw keyboard mode.

Depending on the application, you may be able to use libSDL which has the ability to receive raw keyboard events.

Community
  • 1
  • 1
Andrew Edgecombe
  • 39,594
  • 3
  • 35
  • 61