Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Input.KeyDown problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Paul




PostPosted: Sun Mar 14, 2004 10:39 am   Post subject: Input.KeyDown problem

Im trying to get the program to detect if a predetermined letter has been pressed. and the predetermined letter is going to be a variable. I don't have alot of experience with this so I dunno if this is the way to do it. And I got an error.
in short this is it:
code:

var word, letter: string:=("horse")
var CL: int:=1
var chars : array char of boolean
letter:=word(CL)
loop
Input.KeyDown (chars)
if chars (letter) then
exit
end if
end loop

In here, Im trying to check if the user has typed the frist letter of horse, which is h, and if h has been pressed, it exits the loop. Since this is during another part of the program, Im not using getch.

but I recieve an error on the line:
code:

if chars (letter) then

of array subscript is out of range
I looked at the reference, and I thought if chars('t') works why shouldn't letter, which is a string with the length of one work?
Im baffeled, since if I use chars('h') it works, and if I use chars(letter) it won't, even though letter = h. Im putting a halt on what Im doing currently cause of this Sad
Sponsor
Sponsor
Sponsor
sponsor
recneps




PostPosted: Sun Mar 14, 2004 10:45 am   Post subject: (No subject)

KeyDown uses "char"s they're declared like this
code:
var letter:char:=chr(numberofcharhere)

from there, just go to turing reference to find the codes. Eg.
code:
var up:char:=chr(200)
is the code for a variable to represent up arrow. Got it? Wink
Paul




PostPosted: Sun Mar 14, 2004 10:49 am   Post subject: (No subject)

oh, so I'd have something like this:
code:

var word, letter: string:=("horse")
var CL: int:=1
var chars : array char of boolean
letter:=word(CL)
var let:char:=chr(ord(letter))
loop
Input.KeyDown (chars)
if chars (let) then
exit
end if
end loop

it works, so, the chars (thingy)
thingy has to be a char?
recneps




PostPosted: Sun Mar 14, 2004 12:24 pm   Post subject: (No subject)

noooooo look in turing reference, theres a thing on front page that says "keycode values returned by...." clikc that and it give you the number for each key....
ex.
code:
var a:char:=chr(97)
var A:char:=chr(65)
var chars:array char of boolean
loop
Input.KeyDown (chars)
if chars(a) then
put "You pressed key # 97, otherwise known as 'a'"
elsif chars(A) then
put "You pressed key #65, otherwise known as 'A'"
end if
end loop


Understand it a tad more?
Paul




PostPosted: Sun Mar 14, 2004 12:28 pm   Post subject: (No subject)

Don't have to understand it, it works now Razz
Cervantes




PostPosted: Sun Mar 14, 2004 12:29 pm   Post subject: (No subject)

hey paul. You don't need the part with letters and CL in there any more.

it works like this:
code:
var word : string := ("horse")
var chars : array char of boolean
var let : char := chr (ord (word (1)))
loop
    Input.KeyDown (chars)
    if chars (let) then
        exit
    end if
end loop
Paul




PostPosted: Sun Mar 14, 2004 12:34 pm   Post subject: (No subject)

I know, but CL changes, just look at my finished program in the source code section, its not that well coded but meh.
recneps




PostPosted: Sun Mar 14, 2004 12:38 pm   Post subject: (No subject)

Well at least you got it working Smile Thats what matters.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: