×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Peek for Keyboard Input?
2

Peek for Keyboard Input?

Peek for Keyboard Input?

(OP)
Browsing through many games I have seen, I wonder what PEEK(1047) does and how to use it.  I think it's for keyboard input but I'm not sure.  Is it better to use PEEK or INP when it comes to keyboard input? (I saw it used like so: If PEEK(1047) AND 1 then blah blah blah)

RE: Peek for Keyboard Input?

PEEK usually deals with memory, and the keyboard is a port.  The locks are stored in memory, it might deal with one of them (CAPS, Num, Scroll...).  

RE: Peek for Keyboard Input?

    The way this is written can be explained in this way
CONST True = -1 , False =0
 A%=-1
If a% then Beep else End

Now this seems illogical until you realize that this is a shorthand way of writing           
If a% = True then End

Now looking back at the problem we can read it as this

If [Peek(SomeContentOfMemory)] = -1 then end

AND is a bitwise operator that can compare 2 items
Simply it means that if both arguments are TRUE then the Result is true ,so carried out.
 This table explains the AND operator
Bit in 1st Expression    Bit in 2nd Expression    Bit in Result
    1            1            1
    1            0            0    
    0            1            0
    0            0            0

Hope this helps (btw BASIC uses TRUE as -1 and FALSE as 0 intrinsically) as written in the official QB4.5 Original manual's MicroSoft programmes.

The small code you pointed out simply deletes the keyboard buffer , as most "FAST" games use a keyboard handler to speed up the key reading and do not generally remove keystrokes from the buffer.Hence the computer starts that annoying beeping to indicate too many keys.
Mick

RE: Peek for Keyboard Input?

*Note:

False is 0

anything other than 0 is True

the reason -1 is considered to be true is this:

0 = 00000000 in binary
-1 = 11111111 in binary

so if you say:
False = 0
True = Not False

True will equal -1

but you can actually use anything <> 0 to be true

Try this

CODE

A = 5
If A then
  Print A
Else
  Print "You Can't See Me!!!"
End If
A = 0
If A Then
  Print "You Can't See Me!!!"
Else
  Print "Not 0 = ";(Not A)
End If

Have Fun, Be Young... Code BASIC
-Josh

http://cubee.topcities.com

PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.

RE: Peek for Keyboard Input?

-1 = 11111111 in binary

Are you sure? I think it's

-1 = 10000001

but I could be wrong.

RE: Peek for Keyboard Input?

Actually for 16bit integers it's:
1111111111111111
(but I was making it a little shorter to 8 bit for the example )

The reason is due to the way the processor does it's math

If you want a Quick Reference, open up your windows calculator and set it to scientific mode. press -1 then click the binary radio button.

you get something like:
1111111111111111111111111111111111111111111111111111111111111111
(which is 64bit)

-2 is:
1111111111111111111111111111111111111111111111111111111111111110

-3 is:
1111111111111111111111111111111111111111111111111111111111111101

The reason for this (in 8bit) is:
-1 + 1

CODE

  11111111
+ 00000001
----------
 100000000
The 1 is pushed off to the left, and since it is only 8 bits it leaves you with 00000000 or 0

so...
 -1 + 1 = 0

here are a few other examples:

CODE

  00000001  (1)
+ 00000001  (1)
----------
  00000010  (2)

  11111111  (-1)
+ 11111111  (-1)
----------
  11111110  (-2)

(-2) + 5
  11111110  (-2)
+ 00000101  (5)
----------
  00000011  (3)

Hope this help... In one way or another...

Have Fun, Be Young... Code BASIC
-Josh

http://cubee.topcities.com

PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.

RE: Peek for Keyboard Input?

BTW...

As for -1 = 10000001 in 8 bit...

if the first bit is the sign bit...

00000000 = 0
00000001 = 1
01111111 = 127
10000000 = -128
11111111 = -1

so...
10000001 = -127

Don't worry, I thought the same thing for YEARS... until I started to get deep into Assembly and Machine Language for compilers and emulators...

It is common to think that by revesing the sign bit you get the opposite, but as you can see above, that is not the case...

It's just one of those things you never really think about in high level languages... But will get your @$$ handed to you in the LOW level languages if you don't know how it works...

Have Fun, Be Young... Code BASIC
-Josh

http://cubee.topcities.com

PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close