Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Keyboard Control

Author
Message
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 23rd Aug 2007 16:10
I'm struggling to get the DB to register when multiple keys on the keyboard are pressed. Letter keys are defined by an ASCII code while system keys use DB's Input commands - you know, "ControlKey()", "Shiftkey()".

My problem comes when I use combinations of special keys and letter keys - tell the program to look for the up key and ASCII 100 (letter "d"), and it'll only register if you press the up key and then the letter d. Pressing the two together, or the letter and then the upkey has no effect whatsoever.

Is there a way to stop DB doing this? Whenever two system keys are pressed, it never matters what order they were pressed in. (By the way, the combination of letter and either shift or control doesn't suffer from this problem because the ASCII codes of the keyboard change, so you are, in effect, pressing just a single letter).

Here's my code:



Any help will be gratefully appreciated. Thanks in advance.

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Aug 2007 16:56
You're right, ASCII codes are only for alphanumeric characters.
There is an alternative, use the KEYSTATE(#) command to detect the state of any key on the keyboard
The SCANCODE() command will return which key is being pressed.

I couldn't seem to find a list of all key numbers so I made one.
I've tried to put them in a logical order.


Your signature has been erased by a mod because it was rubbish.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Aug 2007 18:19
Quote: "The SCANCODE() command will return which key is being pressed."


There is a problem with SCANCODE() in this particular scenario. The lower the scancode() the higher the priority. Scancode will only read one key at a time at it will always be triggered by the lowest scancode returned. Run the following example. If you hold down the 5 key for instance, and then press the 1 key, the scancode returned will switch to the 1 key. If you hold down the 1 key and then press the 5 key, the one key will remain and the 5 key will not register.



The design can probably be looked at like a loop from 1 to 255. As soon as a key is returned, it jumps out of the loop and returns that number. Since the lower numbers are first, it will always return those over higher numbers.

Quote: "There is an alternative, use the KEYSTATE(#) command to detect the state of any key "

There's the solution! In this case, you want to know specifically what keys you are going to look for. Since Obese provided the list of scancodes, it should be pretty easy to code.
In the following example, there is a check for keys 1,2,3,4, UP KEY and D. You can press any of them in any combination and the program will tell you which are being pressed.



Enjoy your day.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 23rd Aug 2007 18:27
Sounds great, thanks guys.

On a minor point, will the keystate numbers still be identical on different styles of keyboard(i.e. Qwerty, Dvorak)?

On our way 'ome, on our way 'ome...
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Aug 2007 18:47
I assume hardware should be consistant. A key press is realized by a hardware electric short on the board so I think if the physical layout is the same the triggers should be the same. Not sure though. You know it's hard to make a universal program that everybody can use under every circumstance.

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Aug 2007 19:51 Edited at: 23rd Aug 2007 19:54
Quote: "There is a problem with SCANCODE() in this particular scenario. The lower the scancode() the higher the priority. Scancode will only read one key at a time at it will always be triggered by the lowest scancode returned."

I thought I would include SCANCODE() as it is related to KEYSTATE(#).
Didn't know about the prioritising though.

I was using YaBASIC for the PS2 (came free when I bought PS2, it's very buggy though and primitive ) and each button has a unique binary type number. e.g.
Quote: "
Triangle = 1
Circle = 2
Cross = 4
Square = 8
"

That way it can detect multiple button presses at once.
This is also how the mouse buttons work in DB
Quote: "
Left Click = 1
Right Click = 2
Wheel Click = 4?
"

So then 3 would be both left and right buttons.

I wouldn't worry about different keyboard types, although if you have a Dvorak keyboard you could make a scancode list for it (like my qwerty one) using the SCANCODE() command, though I'm pretty sure the positions are the same.

Your signature has been erased by a mod because it was rubbish.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 25th Aug 2007 14:44
Ok, I'll work on the assumption that all keyboards have the same scancode layout. I don't actually have a Dvorak keyboard (I use QWERTY too), I was just curious to see if my program would still work with a different keyboard. Perhaps it's just a case of the physical keys are exactly the same, and it's the letters printed on them that change between keyboard types...

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Aug 2007 15:57
Quote: "Perhaps it's just a case of the physical keys are exactly the same, and it's the letters printed on them that change between keyboard types..."

That's what I was trying to say
Not 100% sure though

Your signature has been erased by a mod because it was rubbish.
Binary Agents
19
Years of Service
User Offline
Joined: 3rd Jun 2005
Location: Everywhere
Posted: 25th Aug 2007 17:48
You might find this article I wrote a while ago interesting too. It explains the hardware restrictions for keyboards and why its often difficult to detect multiple keys held down at once.

http://forum.thegamecreators.com/?m=forum_view&t=92911&b=1
gearce
18
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 26th Aug 2007 06:19 Edited at: 26th Aug 2007 06:51
Hi there!

Pays to look through previous postings for helpful information and, when you do find something, it pays to make a note of where it was. I didn't and now I can't find it again so, regrettably, I cannot give credit where it's due.

Anyway, here's something I found some time ago in the forum here at TGC and which I posted on TGPF

Quote: "
Not to mention that it's bad to assume that e.g. scancode 21 is what you get when pressing Y. The keyboard scancodes are not tied to letters. On a German keyboard for example, pressing Y would give you scancode 44. So when writing your own keyboard input system, you should use inkey$() instead."


and a reply from TDK_Man

Quote: "I had to point this out a long, long time ago because I use a Spanish keyboard. It's still QUERTY - as opposed to AZERTY, but many keys are in a different place to a UK or US keyboard.

For example, if I want plus and minus (+ and -), the + is on the far right of the QWERTY row of keys next to the Enter key and the - is on the bottom row next to the right Shift key.

In your programs you would map those characters to the ScanCodes of the keys those symbols appear on your keyboard - completely different keys to mine!

This is because ScanCode() is key-based and returns the value of the physical key that is pressed. As Gearce says, there's no guarantee that every keyboard has the same character printed on that key.

Barrie"


It's easy to forget that what you write is read by people world wide.

gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... Some Eskimos have been known to use refrigerators to keep their food from freezing.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Aug 2007 15:40
I don't understand why there isn't just a standardised keyboard layout, what difference does it make?

Your signature has been erased by a mod because it was rubbish.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 4th Sep 2007 18:53
Sorry to resurrect an ancient (ancient?) post like this, but I thought I'd had keyboard control sorted - unfortunately not...

I've set the program up so the user can now assign either one or two scancodes to a particular function, and the other day I started upgrading the program to use these scancodes instead of the Upkey Shiftkey commands.

Here's the problem: say there's one command triggered by an arrow key, and another triggered by shift and the arrow key. Pressing the arrow key works fine - the function I've written checks to see if the specified keystate is true and if so it allows the program to proceed with the command. Press shift and the arrow key, though, and you'll trigger both commands.

I understand that Keystate(arrowkey) = 1 in both cases and so both commands are executed. Is there a way to stop this kind of thing from happening?

I did try setting it so that when a single key is pressed, the program checks to make sure no other keys are being pressed. That did actually work, but for something like camera control, I'd like to be able to use the upkey to move forward while turning left or right using the left and right arrowkeys (Or using the W, A and D keys respectively). Any ideas?

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Sep 2007 19:28
I don't really see what the problem is here, it seems to be doing everything you want it to.

In programming, nothing exists
luke810
18
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 4th Sep 2007 19:35
Use an if - else - endif statement:

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Sep 2007 20:58
@Luke
That way the shift key action couldn't be activated on it's own.

A good way to do this is mathematically.
Here's a unique key press example:


that will only perform "blah blah" if the upkey and only the upkey is pressed.

In programming, nothing exists
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 5th Sep 2007 17:43 Edited at: 5th Sep 2007 17:44
I don't think I explained my problem clearly enough, there seems to be a bit of confusion. I don't want to use DB's standard Upkey(), Downkey() functions. I need to use Scancodes.

Here's a bit of code illustrating what I'm trying to do. Press the upkey, and then press Upkey and Left shift and you'll see that it doesn't work.



So, I tried this instead:



The only difference is that now whenever a single key is pressed, the function I wrote checks all the other keys to make sure no others are being pressed. This should be great... but isn't. With this code snippet, try pressing the rightkey and the upkey at the same time, you'll see what I mean. I'd like to press the upkey and the rightkey and get both messages appearing, in the same way you can say:

If upkey() =1 then blah blah one
If rightkey()=1 then blah blah two

and if you press the upkey and the rightkey, blah blah one and blah blah two both get executed.

I just can't figure out how to arrange this, especially when I have no way of knowing whether the user will assign one scancode to the function or two. Help???

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 5th Sep 2007 21:35 Edited at: 5th Sep 2007 21:42
I sort of mashed this together, so it's not great, but it works.
It allows you to set how many keys you want to use and then you enter each key.
If you have epilepsy you might want to change the colour change effect lol.



It's a bit neater now, I used the cls_keys(0) to store the number of keys.

In programming, nothing exists
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 6th Sep 2007 21:28
Your code's great (Parts are a lot simpler than stuff I'd been using elsewhere in my code), but then I tried to modify it to fit my purposes and... well, I still can't get it to work. Here's my modified version of the code:



Set the two keys at the start to be Q and W - the second key combination I added was just Q on its own. You'll see that both actually happen. it's illustrated more clearly in this one:



Set the requested key combination to be Q and W, and then press them. In theory, W Rotates the cube about the X axis alone, QW rotates the cube about the Y axis alone. In practice, pressing QW makes the cube rotate about the X an Y axis. Am I just trying to do the impossible here?

On our way 'ome, on our way 'ome...
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 7th Sep 2007 01:33 Edited at: 7th Sep 2007 01:47
Possibly


It's not the actual code just how I think you should do it. Haven't tried it yet though. Hope it works...

EDIT:

Something like this should work



but you'll have to figure out how to put it into your code exactly because I'm still not quite sure what it's trying to do...

http://jamesmason01.googlepages.com/index.htm
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 9th Sep 2007 17:11
I've got it (mostly) working!

The introduction of a new variable ("allowed") did the trick - the program cannot scan for a single key press if two keys are being held down.

There's just one snag - here's my code:



Press upkey to YRotate the cube and change the backdrop colour. Press Shift and Upkey to XRotate the cube.

The instant you press Shift and Upkey, the backdrop will change colour for a cycle before reverting to normal. What am I overlooking?

On our way 'ome, on our way 'ome...
Dark Dragon
17
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 9th Sep 2007 17:19 Edited at: 9th Sep 2007 17:21
looks good! What am I overlooking? Dont Know!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 9th Sep 2007 20:39
DD you're posting pointless things again!

In programming, nothing exists
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 10th Sep 2007 01:54
Quote: "DD you're posting pointless things again!"

ah, but is it not pointless to point out pointlessness?


Maryville Game Developers
Visit our website at http://www.freewebs.com/maryville-game-developers/index.htm
luke810
18
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 10th Sep 2007 05:47
Quote: "ah, but is it not pointless to point out pointlessness?"


Not if it helps teach him a lesson.
Insert Name Here
18
Years of Service
User Offline
Joined: 20th Mar 2007
Location: Worcester, England
Posted: 10th Sep 2007 18:13
Quote: "ah, but is it not pointless to point out pointlessness?"

If this is true, then it must be pointless to point out the poitlessnes of the pointing out of the pointlessnes, rendering your post pointless. This means you were wrong to post.
Quote: "Not if it helps teach him a lesson."

If this is true, thn your post was wrong as well. Lose-lose!

Acording to Grandma, I am now enemy of the evil republic, who captured Paris Hilton and couldn't manage it.
Pincho Paxton
22
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 10th Sep 2007 18:51
There's a point to posting pointless posts, and that is to get the point accross that the previous post was pointless.

Now say that when you're drunk!

Insert Name Here
18
Years of Service
User Offline
Joined: 20th Mar 2007
Location: Worcester, England
Posted: 10th Sep 2007 19:17
Your asking me to get drunk? Child abuse!
Hmm... I need to calm down... I'll have a beer.

Acording to Grandma, I am now enemy of the evil republic, who captured Paris Hilton and couldn't manage it.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 10th Sep 2007 21:33 Edited at: 10th Sep 2007 21:33
Erm... I'm all for putting a stop to posting pointless posts, but how about posting some posts that have a point to them?

I'm still struggling with my keyboard here...

On our way 'ome, on our way 'ome...
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 10th Sep 2007 22:18
Why do you colour the backdrop in the first two blocks in the loop but none of the others?

http://jamesmason01.googlepages.com/index.htm
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 10th Sep 2007 23:36
Oh, that was to highlight my problem.

Pressing the upkey Y-rotates the cube. Pressing Shift and Upkey X-Rotates the cube, but for about one frame only it Y-Rotates the cube too.

It's hard to see this if you don't look really carefully, so I tried to make it more obvious - press upkey on its own and the backdrop changes colour. Press shift and upkey, you'll see a flash of colour, and that's what I'm trying to get rid of.

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Sep 2007 23:46
@RTR
Are you doing a 3 button challenge or something?

In programming, nothing exists
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 11th Sep 2007 04:39
@Robert

I'm a little confused about your ultimate goal. Is it to allow users to assign any key combination to controls? Or is it to program multiple keypresses to perform different commands, or both?

Enjoy your day.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 11th Sep 2007 17:02
@Obese
No, as three keys don't always work - the article by Binary Agents (several posts back) explains why. two keys work fine, though, and that's what I'm trying to do.

@Latch
My goal is to allow users to assign any key combination to the controls.

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th Sep 2007 18:38
What I think you should do is store which keys the user is using, then check how many keys are being pressed; then you can check the commands assigned exclusively to that number of keys.
i.e.
Clear Screen = C+L
Copy = C

User presses C+L so the program only checks for the 2 key commands and disregards Copy even though the C key is pressed.

Any use?

In programming, nothing exists
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 11th Sep 2007 21:08
That's exactly what I've been trying to do. The example I gave was a simple version - I've already created an EXE that lets the user specify key commands and export them to a data file. My main program will (I hope) eventually read the file and check the assigned commands.

To use your example (and my code the way it is now), pressing C+L will always trigger C. It will only be for one cycles through the main loop, but it will always trigger C.

If only I could come up with a way of finding out exactly how many keys were being pressed, then I could follow your advice...

On our way 'ome, on our way 'ome...
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th Sep 2007 23:51 Edited at: 11th Sep 2007 23:56
That's not too hard, think of it mathematically.

We have two keys:-
A and B
we have three commands:-
A = Draw a Red Line
B = Draw a Green Circle
A+B = Clear Screen

So our code would be like this:-


I think there's an even simpler way so I'll come back to you on that

[edit]
My new idea didn't work

In programming, nothing exists
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Sep 2007 13:25
@Robert
Create 2 arrays:

one dimensioned to the size of possible scancodes
Dim Array1(255)

one dimensioned to the size of custom assigned keys
Dim Array2(num of custom keys that have actually been assigned)

Assign your controls' bit values:
ex:
up = bit 0 = 1
down = bit 1 = 2
right = bit 2 = 4
left = bit 3 = 8

Have the user select keys for the controls. The key they select has a scancode. This is an index in the first array. The value assigned to each index is the control bit value.

After the user has selected the custom keys, run a loop to assign the custom scancodes to the second array's indexes. This will end up being the list of custom key scancodes.

The keystate command can be used with the second array to test if the custom key is on (1) or off (0). This allows you to bypass having to use the SCANCODE() function and from mapping each key with a number (as you did that when the user assigned their custom keys)

Once you've tested the keystates of the second array, if any are on, then you use the value in this array (which should be the scancode) as the index of the first array, to retrieve the control bit.

Store the control bit in a variable, and use the bitwise or | against any multiple keypress to turn on multiple bits which represent multiple commands. This works a little better than addition in this case because it will turn on specific bits indentified by the controls. This will allow you to test later if these bits are on so that you can execute the commands that are paired with the control (like rotation or position or whatever).

Once you have your variable that is storing all of the keypress bit controls, check which bits are on by using the bitwise and & .

For example: the W key is assigned the up value and the D key is assigned the right value. They are both pressed.

Ahead of time, you would have stored the control bits in the index of the first array that is equal to their scancodes:

Array1(17)=up=1
Array1(32)=right=4

In the second array, you would have stored all of the custom key values to reduce lookup time later

Array2(1)=17
Array2(2)=32

Check the keystate of the custom keys and if on, OR the control variable with the control bit value stored in array 1

if keystate(array2(count))=1
control=control | Array1(Array2(count))
endif

Now that we know what controls are being pressed (i.e. all of the control bits are now stored in the variable control), we AND each bit against our commands and execute the appropriate code:

if (control & up) = up then go up
if (control & right) = right then turn right

I have an example prepared, but see if you can work out what I'm talking about.

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Sep 2007 13:50
@Latch
Oh yeah, duh. I've been using that technique for ages and I didn't think of it

In programming, nothing exists
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 13th Sep 2007 10:47
Hi guys, sorry I've been a day or two in replying, I've had a lot on lately.

@Latch

I sort of understand what you're saying, but I lose it when you start talking about using the bitwise or | to check multiple key presses. What does the "|" symbol actually do? I've never seen it used in a calculation before. Is it a way of constructing binary numbers if you supply values 1, 2, 4, 8, 16, etc.?

@Obese

I was having a look at the code you posted. I've tinkered with it a bit and I've got it to do exactly what I wanted it to! The only thing is, I can't quite figure out why it works...



On our way 'ome, on our way 'ome...
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 13th Sep 2007 19:47
@Robert The Robot
Controlling bits is very useful when you need to detect or initiate an event. Since a bit has only 2 values, 0 and 1, it allows you to act on either of those two cases. You can think of them as OFF and ON switches or FALSE and TRUE conditionals. Since DBC uses 32 bit numbers, you have 32 individual switches that can be turned on and off.

Quote: "Is it a way of constructing binary numbers if you supply values 1, 2, 4, 8, 16, etc.?"


Well, yeah, I guess - it depends on your goal. Let's use whole numbers (unsigned integers) as an example since they are pretty easy to deal with. Whenever you have a number in decimal, your computer looks at as binary (no mystery there). As in your question, starting from right to left, the bits range from 2^0 to 2^31 so they have a maximum value of 2^31 ... 16,8,4,2,1 . You can query and control the bits' ON and OFF status by using the operators "&" and "|" . This differs from using operators such as + and - because these may turn on and off bits that you do not intend.

"|" will compare two numbers and turn on any bits within EITHER of the two numbers that are already on:

| (oring two numbers)
1001011
1100100
-------
1101111

"&" will compare two numbers and turn on any bits that are on within BOTH of the two numbers that are already on:

& (anding two numbers)
1001011
1100100
-------
1000000



The "+" operator in the above example turns on an extra bit, and turns off one of the other bits while "|" and "&" only work on the bits contained in both numbers.

So, you can store the state of your custom keyboard controls using | in a single variable and then query them using & to find the state of specific bits.

Ahead of time, you decide which control belongs to which bit by assigning the control a bit position within a number based on powers of 2
up = 2^0 = 1
down = 2^1 = 2
right = 2^2 = 4
left = 2^3 = 8

So if up and right are pressed, direction = up | right therfore direction = 5

Now you can query direction to see if up was pressed
if (direction & up) = up then etc
Same with right
if (direction & right) = right then etc

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 14th Sep 2007 03:35
@Latch
Nice example, I've never used actual bits before (Except in memblocks).
The buttons on a Playstation controller are controlled like this.

In programming, nothing exists
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 14th Sep 2007 14:45
Yes, very useful. I'll have to remember this, it'll come in handy sometime...



On our way 'ome, on our way 'ome...
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 17th Sep 2007 05:04
Can you not simply add a little timer delay which starts as soon as the first key is pressed (and it is stored). At the end of a set amount of time, it then drops into checking what keys are being pressed.

That way, if D is self destruct and D+F is eject, then the timer delay gives you time to press the F key before the ship blows up!

Not sure if any delay would be detrimental in the actual program though...

TDK_Man

Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Sep 2007 22:29
I'm back again, and still stuggling... <groan>

I thought I'd had it solved, but it appears not (for the umpteenth time).

This code:


works. This code:


doesn't. The only difference is that the keystate values are hardcoded into the first example, and stored in arrays in the second. That's the only difference.

Why won't it work??? This is driving me crazy!
(Now you know how we mad scientists get that way...)

@TDK
I don't think a timer delay would do my program any good.

On our way 'ome, on our way 'ome...
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Sep 2007 22:46
Right off the bat,

In your second snippet, you are adding two different values than you are in the first snippet:

A = keystate(200)
B = keystate(42)
Keys = A+B

A = keystate(KeyCom(1, 1)) = 200
B = keystate(KeyCom(1, 2)) = 1
Keys = A+B

Enjoy your day.
Robert The Robot
18
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 24th Sep 2007 15:05
Hi Latch, sorry I was a while in answering. No, it isn't the numbers that are the problem. Specifying 200 and 42 for both command one and two seems to do exactly the same thing. When the program is told that only one key is pressed, I think it only checks for the keystate of the first value stored in the array.

However, i've been tinkering with the code some more and I've come up with a much better version than the one I had before (i.e. one that actually works...)

Here it is:



There's just one snag with it (isn't there always...)

Press shift and upkey and you'll get one red line then loads of green lines. I want just loads of green lines.
I think I've found the problem - it's this bit at the start of the function:

If A+B <> 2 then CanPressKey(0) = 1

What I think is happening is that when the two keys are first pressed, A+B=0 since the variables are newly created so the program checks for and executes Command One. It then checks for Command Two, realises that this should be executed and then sets CanPresskey(0) to 0, preventing command One from being executed again.

The only flaw in my reasoning is that it relies on the Variables A and B being global, but if they were in a function shouldn't they be local?


I feel like I'm back to square one, all I need to do is eradicate that first red line. Any ideas, please?

On our way 'ome, on our way 'ome...
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 24th Sep 2007 21:00 Edited at: 24th Sep 2007 21:01
OBese87 - You NEED to change your SIG. That Media Fire Junk is a 1024x768 Bitmap when thier server goes offline - that makes for HUGE HUGE long Forum and WAY to much mouse wheel.

[edit]Jees - some are 800x600 some 1024x768... Hmm... if you need free hosting that bad I'll host your sig.[/edit]

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 24th Sep 2007 22:29 Edited at: 25th Sep 2007 11:29
@Robert The Robot

I think the snag in your reasoning is that you are performing operations ON the values returned by the keystate() function. This can give you trouble because the values returned are either 1 or 0. These are switch values, and adding together the on or off states of switches doesn't tell you which switches are on or off. You chould focus more on what happens when these keys are pressed and query the status of the switch, but not necessarily use it's value in the computation (unless used bitwise IMO ).

For example, if you were moving a car around using the keyboard, you might have upkey assigned to move the car forward. While you do check the state of upkey, you generally don't send the command MOVE OBJECT car UPKEY(), but you might have a value that is created or used when upkey is detected independent of the value of UPKEY():
if upkey()=1 then speed=speed+1
move object car,speed

Speed, in this case, is the independent variable influenced by, but not containing the value of, the switch UPKEY().

Here's a basic method to do what I think you are trying:


Quote: "Yes, very useful. I'll have to remember this, it'll come in handy sometime..."


[EDIT] I originally wrote a a somewhat detailed example of using the bit storage method but It is just a more complicated version of what I wrote above so I removed it.

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Sep 2007 23:03
Quote: "OBese87 - You NEED to change your SIG. That Media Fire Junk is a 1024x768 Bitmap when thier server goes offline - that makes for HUGE HUGE long Forum and WAY to much mouse wheel."

It's not my image

In programming, nothing exists
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 26th Sep 2007 01:24
It's your Sig - evidently hosted on a free web site -

Login to post a reply

Server time is: 2025-05-31 21:16:48
Your offset time is: 2025-05-31 21:16:48