inkey$ question

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Captainlazy
Newbie
Posts: 8
Joined: Tue Oct 11, 2005 12:39 pm
Location: UK

inkey$ question

Post by Captainlazy » Thu Oct 13, 2005 1:52 pm

Does anyone know the numbers of each keyboard character. I want to make a game character move with a single keypress but im not entirely sure how to do it. I seem to remember it being done by entering the the keyboard character number. Any ideas??

i know this is wrong but could someone correct this for me please...

IF inkey$ = "q" THEN characterposition = characterposition + 1
IF inkey$ = "w" THEN characterposition = characterposition + 2
IF inkey$ = "e" THEN characterposition = characterposition - 1
((ETC))

I will also need to know how to make it so that the program will not react to keypresses that i do not have a function for - eg. if they press H, the program will not do anything!!

Hope someone can help!!

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Thu Oct 13, 2005 2:34 pm

I'm not really sure what you mean..

The ASCII codes? There's a table in QB, Help->Content->ASCII
IF inkey$ = "q" THEN characterposition = characterposition + 1
IF inkey$ = "w" THEN characterposition = characterposition + 2
IF inkey$ = "e" THEN characterposition = characterposition - 1
Is not wrong at all, it's just how you'd do it.. Unless I'm missunderstanding you..
I have left this dump.

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Thu Oct 13, 2005 3:00 pm

Here's some standard keyboard keys I use everywhere (make the code more readable ;-). followed by a small sample how to use them.

Code: Select all

CONST KeyF1                = 59
CONST KeyF2                = 60
CONST KeyF3                = 61
CONST KeyF4                = 62
CONST KeyF5                = 63
CONST KeyF6                = 64
CONST KeyF7                = 65
CONST KeyF8                = 66
CONST KeyF9                = 67
CONST KeyF10               = 68
CONST KeyF11               = 133
CONST KeyF12               = 134
CONST KeyEscape            = 27
CONST KeyInsert            = 82
CONST KeyDelete            = 83
CONST KeyHome              = 71
CONST KeyEnd               = 79
CONST KeyPageUp            = 73
CONST KeyPageDown          = 81
CONST KeyUpArrow           = 72
CONST KeyDownArrow         = 80
CONST KeyLeftArrow         = 75
CONST KeyRightArrow        = 77
CONST KeyBackspace         = 14
CONST KeyTabulation        = 15
CONST KeyLeftShift         = 42
CONST KeyRightShift        = 54
CONST KeyAlternate         = 56
CONST KeyControl           = 29
CONST KeyCapsLock          = 58
CONST KeyNumlock           = 69
CONST KeyScrollLock        = 70

Keycode$ = ""
DO WHILE CanExit = 0
   ' Wait for a key to be pressed
   DO WHILE LEN(Keycode$) = 0
      KeyCode$ = INKEY$
   LOOP
   SELECT CASE KeyCode$
          CASE CHR$(0) + CHR$(KeyUpArrow)
               CharacterYPosition = CharacterYPosition - 1
          CASE CHR$(0) + CHR$(KeyDownArrow)
               CharacterYPosition = CharacterYPosition + 1
          CASE CHR$(0) + CHR$(KeyLeftArrow)
               CharacterXPosition = CharacterXPosition - 1
          CASE CHR$(0) + CHR$(KeyRightArrow)
               CharacterXPosition = CharacterXPosition + 1
   END SELECT
LOOP
This should be the loop you need. In each CASE you can add code to do what you want :-)

Hope this helps
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Thu Oct 13, 2005 4:01 pm

One word (err... statement): ASC.

For expample.

Code: Select all

Hi% = ASC(H)
That would put the ASCII (hence the ASC(II)) code of H into Hi%.
Image

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Thu Oct 13, 2005 4:38 pm

Nathan1993: You forgot your quotes around your H :P

Try this out, captainlazy

Code: Select all

WHILE KEY$ <>  CHR$(27)      'Makes the program stop if ESC is pressed.
WHILE KEY$ = ""
Key$ = INKEY$
WEND
IF Key$ = "(your letter IN QUOTES)" THEN (insert your functions here)
' and more keywords
IF KEY$ <> CHR$(27) THEN KEY$ = ""   'Resets the key holder
WEND
SYSTEM         'Shuts down the program
If you need ASCII codes, just go to your QB's Help Contents and click ASCII Character Codes.

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Thu Oct 13, 2005 5:30 pm

PQBC wrote:Nathan1993: You forgot your quotes around your H :P

Try this out, captainlazy

Code: Select all

WHILE KEY$ <>  CHR$(27)      'Makes the program stop if ESC is pressed.
WHILE KEY$ = ""
Key$ = INKEY$
WEND
IF Key$ = "(your letter IN QUOTES)" THEN (insert your functions here)
' and more keywords
IF KEY$ <> CHR$(27) THEN KEY$ = ""   'Resets the key holder
WEND
SYSTEM         'Shuts down the program
If you need ASCII codes, just go to your QB's Help Contents and click ASCII Character Codes.
I cannot believe I forgot that quote... and that my shift key worked twice in a row! (8 times now)!!!

I must have fixed it.
Image

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Thu Oct 13, 2005 5:33 pm

Nathan1993 wrote:One word (err... statement): ASC.

For expample.

Code: Select all

Hi% = ASC(H)
That would put the ASCII (hence the ASC(II)) code of H into Hi%.
I guess that wasn't your day. You forgot the quotes AND spelled example wrong! :lol: :P

User avatar
Captainlazy
Newbie
Posts: 8
Joined: Tue Oct 11, 2005 12:39 pm
Location: UK

Post by Captainlazy » Fri Oct 14, 2005 2:26 am

[quote="PQBC"]

Try this out, captainlazy

Code: Select all

WHILE KEY$ <>  CHR$(27)      'Makes the program stop if ESC is pressed.


Cheers guys, this is exactly the kind of thing i was looking for.

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Fri Oct 14, 2005 4:49 pm

yay! Finally I'm the hero!!!

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Sat Oct 15, 2005 9:28 am

PQBC wrote:yay! Finally I'm the hero!!!
As if you weren't before? What about that puny windows clone that ACTUALLY fit into your sig, and was less than 1 kb? Heres my version;

Code: Select all

shell "Pause"
Print "Fatal error. Must... die..."
shell "pause"
end
untested... just like windows. unsure if the shells will work, but they do in c++.
Image

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Mon Oct 17, 2005 4:54 pm

Better copy the code while you can... I hear Microsoft is searching for my ass for releasing the source to their cash cow

ROFL :lol:

Guest
Veteran
Posts: 128
Joined: Sun Aug 14, 2005 8:33 pm
Location: Forest Lake, MN
Contact:

Post by Guest » Mon Oct 17, 2005 5:17 pm

No, you're to minor in their to do list for them to even care...

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} » Mon Oct 17, 2005 6:31 pm

PQBC wrote:Better copy the code while you can... I hear Microsoft is searching for my ass for releasing the source to their cash cow

ROFL :lol:
... (searches for a jump drive. Hides it in the vent.)
Image

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Wed Oct 19, 2005 5:41 pm

(uses AutoClave [awesome erasing tool - useful against hackers] in binary overwrite mode[25 layer delete, secure against NASA, takes about a day and a third to run] 5 times for a total of one week)

Let's see Microsoft try to trace me now!

Post Reply