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.

Newcomers DBPro Corner / How does scancode() work when two (three+) keys are pressed?

Author
Message
Dejunai
19
Years of Service
User Offline
Joined: 19th Jul 2005
Location: Mentally: Manhattan; Physically: LA -doh
Posted: 7th Aug 2005 01:23 Edited at: 7th Aug 2005 02:06
How does scancode() work when two (three+) keys are pressed?

It would appear that scancode() returns the ONLY lowest keycode available.

And as such is useless for multiple keys pressed at the same time.

So it seems fairly reasonable that the best way to detect multiple
keys pressed at the same time, is the keystate() function.

As such I couldn't possiblly be the first member of the community,
to desire an optimized piece of code for detecting the state of the
arrowkeys/keypad-arrowkeys and the major players SHIFT, CTRL, and ALT.

I will immediately setout on a quest to develop the perfect piece of code,
but if there are any kind-souled veterens out there, who have done the
tiresome work already, it would be nice to see how they've accomplished
the same task.

I would imagine a the perfected snippet would be valuable to all
newbies.

============ : Windows 200 SP4+, 3.2GHz Hyperthreaded
-Dejunai ............: 2GB RAM, nVidia 6600 256MB AGP 8x
============ : AC'97 basic A-Bit Motherboard Audio ( drivers always Maintained )
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 7th Aug 2005 01:33 Edited at: 7th Aug 2005 01:33
How do you mean optimised ?
You are entirly right though, the keystate command is the best way to go. Many of us generaly use this wee image to help us.

Hope it helps you too

Attachments

Login to view attachments
Dejunai
19
Years of Service
User Offline
Joined: 19th Jul 2005
Location: Mentally: Manhattan; Physically: LA -doh
Posted: 7th Aug 2005 02:04
I usually define "optimized" as a euphemism meaning:
"Better than I can do alone."

Thanks for the image...
I am certain it will come in handy.

============ : Windows 200 SP4+, 3.2GHz Hyperthreaded
-Dejunai ............: 2GB RAM, nVidia 6600 256MB AGP 8x
============ : AC'97 basic A-Bit Motherboard Audio ( drivers always Maintained )
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Aug 2005 12:06 Edited at: 8th Aug 2005 12:09
I'm surprised that altkey() isn't among the keys listed in the commands list ( like controlkey() and shiftkey() ). I've experimented for hours trying to help you but I just couldn't get it.

Perhaps scancode() is bugged. If it would just show the newest key pressed it wouldn't be a problem to hold the last 3 keypresses in an array (my first experiment). But because as you noted that it only shows the lowest scancode number... it will never show ALT being pressed while control is being held down.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 8th Aug 2005 15:51
Thats why you can use keystate()...for instance to check if alt and control were being pressed down:


if keystate(56)=1 and keystate(29)=1 then print "both alt and control are being pressed


or if you like


Function AltKey()
result=keystate(56)
Endfunction result


Hope that helps.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Aug 2005 22:12 Edited at: 8th Aug 2005 22:13
I see. I thought scancode() was like entry$() or inkey$(). Scancode() is a command to only help us determine what keystate to look for. That's what I love about these forums... I learn something new everyday... even when i'm not personally asking questions.

Thanks

Dejunai
19
Years of Service
User Offline
Joined: 19th Jul 2005
Location: Mentally: Manhattan; Physically: LA -doh
Posted: 9th Aug 2005 15:45 Edited at: 9th Aug 2005 15:52
Er, personally I do something like...



The reason I brought it up, was that there doesn't appear
to be a significant way of polling all keys without a
brute force approach. I certainly don't need to poll all keys,
but if I or someone else did, I thought it might be helpful
to bring it up. Discuss different schools of thought. In my
snippet you can press any of the three modifier keys, and
one other key. But you could never press x&y and get a result
other than x. And if I did want to poll all the keys, we're
talking about over 100 brute force IF-statements. And if
I were nutz and planned on making any key combination available
then I'd be setting 3 of 100 flags, and then need to test
100+^3 combinations, and the game would be SPF not FPS.

Obviously such an approach is ludicrous, so either veteren
programmers have developed a better way, or have limited
their approach. I have no idea what I am expecting. I suppose
I was expecting a conversation about the approach, or helpful
tidbits... Which I got and I am thankful for. ( THANKS )

However, I didn't mean for the topic to be a specific request
for help with my code. More or less I was looking for a general
discussion about approch. I suppose I failed to phrase it that
way. Quickly review my original topic, I was also curious, about
any undocumented features I was missing about the scancode()
function. Lets face it, it would be nice to poll a core function
and get a array/list of keys currently pressed. Something to
the tune of:

key1 = scancode(0)
key2 = scancode(1)
key3 = scancode(2)

Of course thats assuming a core function would be faster than
a user made function.

============ : Windows 200 SP4+, 3.2GHz Hyperthreaded
-Dejunai ............: 2GB RAM, nVidia 6600 256MB AGP 8x
============ : AC'97 basic A-Bit Motherboard Audio ( drivers always Maintained )
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 9th Aug 2005 19:06
Ahh I see what you mean...well ive made this little bit of code (sorry, i know you werent asking for code but i think much better by just typing it out) which in theory would do what you want, unfortunatly you come up against the limitations of the computer...it isnt always that acurate with lots of keys pressed.
Its fine with 3-4 sequential presses and can even sometimes manage up to 8 but when many keys are tapped at the same time or you try and press too many keys, the system wont always record them.

That aside, please feel free to meddle with the code below and see what you come up with


Hope it helps

Dejunai
19
Years of Service
User Offline
Joined: 19th Jul 2005
Location: Mentally: Manhattan; Physically: LA -doh
Posted: 9th Aug 2005 19:52
Yeah, yeah...
Code good; I didn't mean I didn't want code...
I just meant I wasn't asking for code specific to my problem.
I would like to see code on how peeps besides me are approaching
what ( to me seems it ) should be a common problem.

And when I say problem, I mean "Math" problem,
not "Oh $h!T, flat tire" problem.

I like your solution, but er well um... 10 keys that's a bit
much by anyones standards...

Heh, and just a tidbit... The OS can track as many keys
as the keyboard reports. And all PC keyboards are required
to report a minimum of 3... For the interuput C-A-Del,
and for the most part cheapo KB manufacturers only report
the 3 required. Nicer keyboards report more. And last but
not least, knock off KB's tend to only report 2, and have
an internal cycle that fakes the standard modifer keys as
one key when pressed in unison. Which I am certain made no
sense. Er, knock offs can't report a+b+c just a+b... and
can only report <shift>+a not <shift>+a+b... However they
can report <crtl><shift>+a, where <crtl><shift> is faked
in the logic of the keyboard. Problably saving them 6.5 cents
on every 10,000 keyboards. Of course then again, it might just
be the opium talking.

============ : Windows 200 SP4+, 3.2GHz Hyperthreaded
-Dejunai ............: 2GB RAM, nVidia 6600 256MB AGP 8x
============ : AC'97 basic A-Bit Motherboard Audio ( drivers always Maintained )

Login to post a reply

Server time is: 2024-11-27 21:47:40
Your offset time is: 2024-11-27 21:47:40