0

I am working on project in which my requirement is to use two arrow keys( like UP,DOWN,RIGHT,LEFT) at same time. How can I read from user if any of two arrow keys are pressed at same time I can read arrow key using getchar() but only applicable to single key.

Any Advice

Thanks in advance

  • I don't think you can do that in a console without writing some assembly code. If you are making a GUI game, then you should use their respective key input methods – debanshu das May 14 '21 at 10:34
  • You can never press two of those keys at the same time. So you cannot read them at the same time. – Paul Ogilvie May 14 '21 at 10:34
  • What you want to do is to get a "key down" event, which isn't possible through standard C and the standard C functions. Once you've found a way to get this "key down" event (and the corresponding "key up" event) then you can use it to keep track of simultaneously pressed keys. – Some programmer dude May 14 '21 at 10:36
  • I remember that when I was a university student, I implemented some kind of frame rate, `FPS` mechanism for the game using a while loop. This way I could get two keys being pressed and so on. I think this is also how game engines work too. – ATheCoder May 14 '21 at 10:47
  • 1
    @PaulOgilvie I don't think your comment is helpful. Pressing 2 keys simultaneously is clearly possible and many programs rely on that. – user694733 May 14 '21 at 10:49
  • I also think that this might be helpful: https://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input – ATheCoder May 14 '21 at 10:52
  • @user694733, cursor keys will be read one at a time. Only modifiers (ctr, alt, shift etc) can be pressed together with such a key. The keyboard driver and hardware normally resolves multiple key presses of non-modifiers. – Paul Ogilvie May 14 '21 at 11:04
  • For which operating system should your code be compatible? Do you want a console mode program or a GUI program ? – fpiette May 14 '21 at 12:11
  • Thanks for replying to all of you guys, – dbgarasiya May 14 '21 at 12:27
  • it is not necessary with c only , if it possible with c++ , then it's ok for me – dbgarasiya May 14 '21 at 12:28
  • i am using Linux OS – dbgarasiya May 14 '21 at 12:29
  • @PaulOgilvie Of course you can press two keys at the same time, nothing can stop you. You can even hold both during the same time. But what you mean is that the event messages for the presses are sent one after the other. This is also true for modifier keys, depending on the level where you have these messages. – the busybee May 14 '21 at 13:41
  • You need to do some research for a library that supports such functionality. As others said, your application or this library needs to trace the events "key pressed" and "key released" for this. -- This issue is mostly not depending on the programming language. -- Is this a console program, or a GUI program? If the latter, which GUI library does it use? – the busybee May 14 '21 at 13:44
  • Thanks for all , I have solved out this problem using this link , https://iq.direct/blog/325-how-to-read-direct-mouse-and-keyboard-events-in-c-console-app.html Hope any one get help from this – dbgarasiya May 17 '21 at 06:33

0 Answers0