Page 1 of 1

System messages

Posted: Sun Feb 04, 2007 10:20 pm
by stuartls
I need alot of input on how system messages work. I kind of understand the intercepting of them and passing them. But why doesn't this work?

on mouseUp
ask "Enter the Question # you would like to go to!"
keyDown
end mouseUp
on keyDown theKey
if theKey is not a number then beep
else beep
end keyDown

It runs, but when someone types in an alpha in the field it doesn't beep. I am confused where this borrowed should go. Should it have been included n the mouseup?. I've read what I could on the system messages as far as trapping them and realeasing them. I can see in my mind the system message being deployed and being sent, but am lost after that.

Stu

Posted: Sun Feb 04, 2007 10:21 pm
by stuartls
"borrowed code"

Posted: Mon Feb 05, 2007 10:35 am
by oliverk
Hi Stuart,

System messages in Revolution are sent to various objects in your program when an event occurs, to allow your program to respond to the message.

I think that your script should work fine, there is nothing wrong with doing it this way and I suspect that either you are not hearing the beep, or there is something else in your program interfering with the message.

A good way to minimise the chance of unwanted interference would be to restructure your code slightly, to something like this:

Code: Select all

on goToQuestion pNumber
  if pNumber is not a number then
    beep
  else
    beep
  end if
end goToQuestion

on mouseUp
  ask "Enter the Question # you would like to go to!"
  goToQuestion pNumber
end mouseUp

on keyDown pKey
  goToQuestion pKey
end keyDown
I find this easier to understand because it means that a keydown message
is always the result of a key press by the user, and is never sent by the script.

If you put a breakpoint at the start of the on goToQuestion handler, you'll be able to see whether the message is being received or not, if its not then it means that the keyDown message is not being received by the field. Possible reasons for this are that you have another keydown handler somewhere in the same script, messages are locked, or there is another object above the field, blocking messages from it.

Hope this helps, please let me know if you are still having problems.

Regards

Oliver

Posted: Tue Feb 06, 2007 3:24 am
by stuartls
Oliver,

For one, thanks for your reply. I took a long look at your code and I appreciate it! Let me ask you this. What is the difference between trapping a keyDown command and using nested if statements to make sure the person types in a number and it is between 1 and 200? Any thoughts?


Stu

Posted: Tue Feb 06, 2007 10:21 am
by oliverk
Hi Stu,

Do you mean what is the difference between checking the value of the key that was pressed inside keydown as opposed to after keydown has been trapped?

If this is your question, I think this is a matter of preference. If I was writing a big program that I knew would become complex and may have more than one person working on it, I would consider checking in both places, and using a function to do so for me, eg:

Code: Select all

on keyDown pKey
  if not validQuestion(pKey) then
    -- Allow the message to continue on its path without taking any action
    pass keyDown
  end if

  goToQuestion pKey
end keyDown

-- Goes to question number pNumber if valid, otherwise tells the 
-- user that the question number is invalid and exits.
on goToQuestion pNumber
  if not validQuestion(pNumber) then
    answer "Invalid question number!"
    exit goToQuestion
  end if
 
  beep
end goToQuestion

-- Returns true if pQuestion is a valid question number, ie it is
-- between 1 and 200.
function validQuestion pQuestion
  if pQuestion is not an integer then
    return false
  end if
  if pQuestion < 1 then
    return false
  end if
  if pQuestion > 200 then
    return false
  end if

  return true
end validQuestion
Regards

Oliver