 |
26-08-2008, 09:12 PM
|
#1 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
use multiple keys in C
hi guys!
How can i use multiple keys in C? i am doing a graphics project. my aim is to move an object from one place to other. for moving to left, right, up and down we can use directional keys. this i have already achieved.
Now i want to move the same object in diagonal direction using multiple keys.
For Eg.:- if i press left and up arrow keys together the object should move to top left corner.
Note:- If the query is not clear then pls reply. do not ignore. Pls help me.
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
26-08-2008, 09:20 PM
|
#2 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
If both keys (1,2) are pressed down:
# Keep increasing both X and Y co-ords of the object by each unit step. (ie) (9,9) becomes (10,10), now repaint.
# OR
# First move one step in 1's direction
# Second move one step in 2's direction
Repeat
This would be the general idea I suppose. I do it this way in Qt...
__________________
Harsh J
www.harshj.com
|
|
|
27-08-2008, 02:13 PM
|
#3 (permalink)
|
|
Broken In
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
|
Re: use multiple keys in C
I don't think that you will ever get a distinct keycode when 2 keys are simultaneously pressed (I might be wrong though). You should first try to output the keycodes that you receive and see if you get a different one when both are pressed.
Instead try using a different key altogether for your diagonal movement.
|
|
|
28-08-2008, 01:10 PM
|
#4 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: use multiple keys in C
@qwertymaniac
can you pls give me the actul code coz when i use the codes it doesn't work
Code:
if(key==77 && key==80)//key is the variable thats gets the ascii of the key pressed
{
}
this doesn't work
@bandu
how to game developers achieve this then? is there no possibility in C?
__________________
eXPerience is what a MAN learn's fROM.....
Last edited by ayush_chh; 28-08-2008 at 02:55 PM.
Reason: Automerged Doublepost
|
|
|
28-08-2008, 06:32 PM
|
#5 (permalink)
|
|
Legen-wait for it-dary!
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
|
Re: use multiple keys in C
Game devs do it using directinput. I havent tried it but that code should NOT work, as a variable cannot equal to two operands(lol, think, think) but you can try this:
if(key == (87 && 79) )
using logical and operator.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
|
|
|
28-08-2008, 06:52 PM
|
#6 (permalink)
|
|
Broken In
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
|
Re: use multiple keys in C
Quote:
@bandu
how to game developers achieve this then? is there no possibility in C?
|
Is there a freely available C compiler that I can quickly download and try all this myself?
I found a free copy of TC 2.0 and quickly worked out a small program to test these things.
Here's what I wrote:
Code:
#include <conio.h>
int main()
{
int myKey;
clrscr();
printf("Press q to quit");
do
{
if(kbhit())
{
myKey = getch();
switch(myKey)
{
case 72: printf("\nMove to top");
break;
case 75: printf("\nMove to left");
break;
case (72 && 75): printf("\nMove to top-left");
break;
case 113: return 0;
}
}
} while(1);
}
As I suspected, you either get a left or a top key press event (the latest that was pressed) and not both - neither a && nor || Nothing.
As for your question - I think the Game devs use some other way to determine simultaneous key press - directinput or some such thing.
If you can elaborate on what exactly you are using (directinput, GDI, etc.), then maybe I can try something more.
- Bandu.
Last edited by Bandu; 28-08-2008 at 07:27 PM.
Reason: Automerged Doublepost
|
|
|
29-08-2008, 06:35 AM
|
#7 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
I use a key state list of 4 boolean values, and switch them T/F at each valid keypress event and keep calling the function that works on them. At the same time it would not work, unless you are also using a modifier...
__________________
Harsh J
www.harshj.com
|
|
|
29-08-2008, 12:46 PM
|
#8 (permalink)
|
|
Legen-wait for it-dary!
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
|
Re: use multiple keys in C
Quote:
case 72: printf("\nMove to top");
break;
case 75: printf("\nMove to left");
break;
case (72 && 75): printf("\nMove to top-left");
break;
case 113: return 0;
|
@bandu, try :
case (72 && 75): blah blah; break;
case 72: blah; break;
case 75: blah; break;
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
|
|
|
29-08-2008, 02:16 PM
|
#9 (permalink)
|
|
Broken In
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
|
Re: use multiple keys in C
@harsh: I have no idea about key state list. Can you post your snippet?
@dheeraj: Have a look at my code snippet above. It does have
Code:
case (72 && 75): printf("\nMove to top-left");
. I never get to see the Move to top-left output.
- Bandu.
|
|
|
29-08-2008, 07:07 PM
|
#10 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
I don't know how a logical AND would work out, bitwise AND & (OR | more like) could make some sense but logical? It always returns 0 or 1...
Here's my snippet in Python and Qt (PyQt4 module's required if you wanna execute it):
Code:
from PyQt4 import QtCore, QtGui
from sys import argv
class MyWidget(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.directions = {
QtCore.Qt.Key_Up:[False,"Up"],
QtCore.Qt.Key_Left:[False, "Left"],
QtCore.Qt.Key_Down:[False, "Down"],
QtCore.Qt.Key_Right:[False, "Right"]
}
self.dims = [0,0,10,20]
self.rect = QtCore.QRect(*self.dims)
self.painter = QtGui.QPainter()
def updateKeys(self, button, value):
self.directions[button][0]=value
def callFunc(self):
a=""
for each in self.directions:
if self.directions[each][0]==True:
if self.directions[each][1]=="Left":
self.dims[2]-=5
elif self.directions[each][1]=="Right":
self.dims[2]+=5
elif self.directions[each][1]=="Up":
self.dims[3]-=5
else:
self.dims[3]+=5
self.rect = QtCore.QRect(*self.dims)
def paintEvent(self, event):
self.painter.begin(self)
self.painter.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
self.painter.setPen(QtCore.Qt.SolidLine)
self.painter.drawRect(self.rect)
self.painter.end()
def keyPressEvent(self, event):
if event.key() in self.directions:
self.updateKeys(event.key(),True)
self.callFunc()
QtGui.QWidget.keyPressEvent(self, event)
self.update()
def keyReleaseEvent(self, event):
if event.key() in self.directions:
self.updateKeys(event.key(),False)
self.callFunc()
QtGui.QWidget.keyPressEvent(self, event)
self.update()
app=QtGui.QApplication(argv)
a=MyWidget()
a.show()
app.exec_()
The bold part indicates the key-state list. I've used a dict, for easiness and readability but you can use a simple bool array too.
__________________
Harsh J
www.harshj.com
Last edited by QwertyManiac; 29-08-2008 at 07:14 PM.
Reason: Formatting
|
|
|
29-08-2008, 08:55 PM
|
#11 (permalink)
|
|
Broken In
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
|
Re: use multiple keys in C
@Harsh - You are right - logical && does not make sense. But, a bitwise & also does not work in this case.
I could install Turbo C, but am not sure about Python.
What would you get if you add a printf (or similar thing) in your def keyPressEvent(self, event) and press both the key simultaneously? What would get printed?
|
|
|
29-08-2008, 09:29 PM
|
#12 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
keyPressEvent is an overloaded function here (Qt, much like Java's own AWT?), whenever a key is pressed on the keyboard while the Widget is the active window, the keyPressEvent()'s contents are executed.
Let me show it in parallel, with a print and the window. Its gonna be quite some amount of screenshots:
On start, the default rect of 10x20 at (0,0) is drawn, as per the code above. Each keypress makes a change of 5 pixels in this case.
On one keypress:
On two keypresses:
Again, on two different keypresses:
Total output on the terminal via print, as you asked:
Code:
Right
Right
RightDown
Right
Up
LeftUp
Up
3 values appear because the input is sent one by one and the same's at removal. That is not an issue or anything
__________________
Harsh J
www.harshj.com
|
|
|
29-08-2008, 09:52 PM
|
#13 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: use multiple keys in C
@dheeraj
thankyou for making me think  .....the logical AND won't work tha's for sure
i thought of one more method
when the right key is pressed the control goes to one more while loop where i can check for next key(up or down) and then use it.
i found some values on net like (45) can be used for (Alt+X)....how is that possible?? coz 45 is ascii code for ' -'
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
29-08-2008, 10:06 PM
|
#14 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
ayush_chh - Cause ALT/CTRL/SHIFT/META/etc keys are known as modifier keys. They modify the general behaviour and values (OR/XOR?) of the keys they are used with.
__________________
Harsh J
www.harshj.com
|
|
|
31-08-2008, 08:39 AM
|
#15 (permalink)
|
|
Legen-wait for it-dary!
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
|
Re: use multiple keys in C
Windoze has something perfect for this called GetKeyboardState, but unfortunately its win32 api.
Do you know if dos supports multiple keypresses? that depends on the OS(I think), like windows does only 3 keypresses simultaneosly.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
|
|
|
31-08-2008, 09:51 AM
|
#16 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
Yep only 3
__________________
Harsh J
www.harshj.com
|
|
|
01-09-2008, 12:35 AM
|
#17 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: use multiple keys in C
thanks a lot for the replies .... that was informative..
i have decided to use num - pad for the control now.....
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
01-09-2008, 12:42 AM
|
#18 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
With the set/reset array I hope? So that two keypresses are usable?
__________________
Harsh J
www.harshj.com
|
|
|
01-09-2008, 08:30 PM
|
#19 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Bangalore
Posts: 487
|
Re: use multiple keys in C
what is this Set/reset array?
__________________
eXPerience is what a MAN learn's fROM.....
|
|
|
01-09-2008, 08:54 PM
|
#20 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: use multiple keys in C
Its the same as your multiple loop idea, and what I've demoed above.
__________________
Harsh J
www.harshj.com
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|