Page 1 of 2

Space Bar [Solved]

Posted: Mon Jul 13, 2009 1:35 pm
by symiant
Hi there,

I have a really simple question. If I want to refer to the space bar in a script, how do I do that? I've figured out the on keydown function, but I can't find anything in the reference material about how to specify the space bar!

Thanks in advance

Space bar

Posted: Mon Jul 13, 2009 1:55 pm
by jmburnod
Hi Symiant,

You can try this :

Code: Select all

on keydown pLetter
   if pLetter = " " then
      doMyScript
   end if
end keydown

Regards

Jean-Marc

Posted: Mon Jul 13, 2009 1:57 pm
by SparkOut
There are a couple of variants you can investigate, keyDown and rawKeyDown. In keyDown the character is sent with the message, and space can be tested using the Rev built in constant "space" :

Code: Select all

on keyDown theKey
   if theKey is space then
      put "You pressed space"    
   end if
   pass keydown
end keyDown
rawKeyDown will send the raw key code value, which for space will be 32:

Code: Select all

on rawKeyDown theRawKey
   if theRawKey is 32 then
      put "Space bar pressed"
   end if
   pass rawkeydown
end rawKeyDown
There are other messages for return, enter, tab etc. The rawKeyDown handler will be especially useful for things like mouse-wheel scrolling, etc.

HTH.


Nice one Jean-Marc, just got in while I was copy pasting my script snippets! :D

Re: Space Bar

Posted: Mon Jul 13, 2009 1:58 pm
by shadowslash
symiant wrote:Hi there,

I have a really simple question. If I want to refer to the space bar in a script, how do I do that? I've figured out the on keydown function, but I can't find anything in the reference material about how to specify the space bar!

Thanks in advance
I also happen to have a really simple answer. If you want to refer to the space bar in a script, you do that by using the keyword space. You've figured out the on keyDown function, so you can specify the space bar by still using the space keyword. Image

Posted: Mon Jul 13, 2009 2:00 pm
by SparkOut
And now it's a game of simulpost tag! :lol:

Posted: Mon Jul 13, 2009 2:01 pm
by shadowslash
SparkOut wrote:And now it's a game of simulpost tag! :lol:
Oooops, sorry, my bad, you see, when I was about to reply, I had to do something else so when I pressed the submit button, it was too late to realize that two answers has already been posted before mine. Image

Posted: Mon Jul 13, 2009 2:02 pm
by SparkOut
No bad! I just thought it was funny. At least symiant now has plenty of answers!
Welcome to the forum!

Posted: Mon Jul 13, 2009 2:07 pm
by symiant
Cheers for the fast replies!

Re: Space Bar

Posted: Sun Apr 28, 2013 12:00 am
by palanolho
Greetings everyone.

I'm having some problems with the keyDown message...

What I have:
- I have a group of objects that I would like to scroll using mouse drag over the group with the aid of a key pressed by the user

What I did:
- I was able to scroll the group using drag and drop by following this simple tutorial (check runrev lesson: 44421-how-to-create-a-scrolling-group-using-the-improved-graphics-architecture)

What I want:
- i want to be able to scroll the group when the user is pressing the SPACEBAR and he is drag & dropping inside the group area

on so, what have i don .. i have modified the simple drag & drop script to only scroll if the SPACEBAR is pressed.

however, it seems the system doesn't even recognizes that i'm pressing the key.

Heres the code in caso someone could give me an help.

Note: this code is added to the group

Code: Select all

local sScrolling
local sInitialMouseX, sInitialMouseY
local sInitialHScroll, sInitialVScroll

on keyDown theKey
     answer theKey
     if theKey is space then
          //answer "SPACE SPACE SPACE"          
          put true into sScrolling
     end if
     pass keydown
end keyDown

on keyUp theKey
     if theKey is space then
          put false into sScrolling
     end if
end keyUp

on mouseDown
    ## Allow the group to scroll
    ##put true into sScrolling

    ## Record the initial touch position
    put item 1 of the mouseLoc into sInitialMouseX
    put item 2 of the mouseLoc into sInitialMouseY

    ## Record the initial hScroll and vScroll
    put the vScroll of me into sInitialVScroll
    put the hScroll of me into sInitialHScroll
end mouseDown

on mouseMove mouseX, mouseY
    ## If the screen is being touched then
    if sScrolling then
        ## Calculate how far the touch has moved since it started
        put mouseY - sInitialMouseY into tVChange
        put mouseX- sInitialMouseX into tHChange

        ## Reset the hScroll and vScroll to follow the touch
        lock screen
        set the vScroll of me to sInitialVScroll - tVChange
        set the hScroll of me to sInitialHScroll - tHChange
        unlock screen
    end if
end mouseMove
Can anyone give me some hint?

Thanks in advance
- Miguel

Re: Space Bar

Posted: Sun Apr 28, 2013 3:58 am
by dunbarx
Hi.

You are confusing a message with a function. When you press a key, like the spacebar, several messages are sent, which you can trap and process. But you cannot use the spacebar in the same way as, say, the commandKey or the optionKey.

There is no "spaceBarKey" function native to LiveCode, as there is, say, to "optionKey"

So you cannot test if "the spaceBarKey is down". All your problems will go away if you move your thinking to one of the control keys that do have this native function available, like command,option, shift, capsLock or control.

BUT, if you are intent on the spacebar, you may want to look at the "keysDown" function. This will allow you to check the state of any key at all, including spacebar.

Craig Newman

Re: Space Bar

Posted: Sun Apr 28, 2013 6:43 pm
by jacque
Actually, space should work okay in the script. The keydown/keyup messsages recognize the constant "space".

The problem may be that the field isn't focused. If the user hasn't clicked on the field or something else hasn't put it in focus, the keydown message will not be sent to the field, it will be sent to the card or to whatever other field might have focus at that moment. If there are no other controls on the card that need to trap keydowns, then a handler in the card script can manage the scrolling instead of a script in the field itself.

To see if that's the problem, try clicking on the field first before scrolling with the spacebar. If that works, move the script to the card.

Re: Space Bar

Posted: Sun Apr 28, 2013 7:11 pm
by palanolho
OK, to archive what I want .. partially lol :)

There's something not working right but I'm not getting what (i have attached the example and code if anyone want to check)

I have put the "space down" verification in the Stack code

Then, on the group code I have placed the drag & drop scroll code that sill only be enabled IF the spacebar is being pressed.

I first tried to make something like a on*off switch. i click space to activate the scroll, and then i can scroll with drag & drop. Then if i click space again, the scroll is disabled and i cannot scroll.
This worked fine. The problem is when i try to do it while the space is being pressed. It works a little bit then stops working. Them i release and press the space again and it works a little bit again and stops again.

Any ideas/suggestions?

I also noticed something weird: if you create an "on keyDown theKey" handler with something like "answer theKey" and is you execute and hold a letter key, the answer dialog will open once and won't open again until you press a new letter. if you press the spacebar, the dialog will appear and disappear continuously while you are pressing the space.

Re: Space Bar

Posted: Sun Apr 28, 2013 10:19 pm
by jacque
Okay, I see. In that case Craig was right, you can't test for the space key that way. He understood what you wanted better than I did. Do what he suggested and use the option or command key, or some other control key that has a built-in function to test whether it is pressed or not.

The reason you are getting multiple responses for the spacebar is because as long as you hold it down, the OS key repeat kicks in and it is sent repeatedly until you let go. The control keys don't do that, they only have "up" or "down" states.

Re: Space Bar

Posted: Sun Apr 28, 2013 11:22 pm
by palanolho
hmmm they why this doenst happen with letter keys for example? the space key is special in this matter ?

ill try with control key then.

Do i use the keyDown handler too? can you give me an example please?

Many thanks
- Miguel

Re: Space Bar

Posted: Sun Apr 28, 2013 11:53 pm
by palanolho
ok ... i think i did it (yeh! ) :)

but i really REALLY wanted to make this with the space bar (like in Adobe Illustrator) :(

There's really no way do do it with space?

thank you for your help guys

cheers
- Miguel