LiveCode: Using the Mouse Wheel with a Option Menu

Back To LiveCode Main Page

I was creating a calendar in LiveCode and I used a Option Menu for my month selection. I wanted to be able to hover the mouse cursor over the Option Menu and roll the wheel on the mouse to change the month. After some experimentation this is what I came up with...

First use the Menu Items window in the Inspector to populate the Option Menu with the names of the months. Just type the names in and hit RETURN after each month to put them on their own line. See Fig. 1 below.

Then I closed the Inspector and opened Code for the Option Menu. You may have noticed in the Inspector that I had named my Option Menu to SelMonth.


Tip for quickly entering the month names into an Option Menu

You can drop a temporary button onto the card and enter the following code within it and then click the temporary button. The Menu Items of Option Menu "SelMonth" will be populated with the names of the months. Afterwards you can delete the temporary button.



I then placed the following code in the Option Menu button "SelMonth"

on rawKeyDown pKeyCode

-- answer pKeyCode --Uncomment to get the raw key numbers from keyboard. Also traps the mousewheel.

-- Get the line number of the month selected and put it into the variable named "monthIndex"
put the word 2 of selectedline of button "SelMonth" into monthIndex

if pKeyCode is 65309 then -- Test if the mousewheel is rolled upward

if monthIndex > 1 then -- If current line number is greater than one (Prevents trying to go before January)
subtract 1 from monthIndex -- subtract one from the variable monthIndex
select line monthIndex of button "SelMonth" -- Set the new selected month to the new number in the variable monthIndex
end if
end
if


if pKeyCode is 65308 then -- Test if the mousewheel is rolled downward
if monthIndex < 12 then -- If current line number is less than twelve (Prevents trying to go pass December)
add 1 to monthIndex -- add one to the variable monthIndex
select line monthIndex of button "SelMonth" -- Set the new selected month to the new number in the variable monthIndex
end if
end
if

pass rawKeyDown -- Pass all information onward. If you don't put this in then your keyboard etc won't work.


end rawKeyDown


About the selectedline

The selectedline will return the line number followed by which object it was retrieved from. I made a button and put the following code within it to tested it on the Option Menu even though it isn't a field. Found it will work.

on mouseUp pMouseButton
answer selectedline of button "SelMonth"
end mouseUp

I had selected July and when I clicked the button the following answer box popped up...

Since the number I wanted was the second "word" I used the following code to retrieve it.

put the word 2 of selectedline of button "SelMonth" into monthIndex

This code is also handy for getting the month number. You don't have to write extra code to test each month name to decide on what number to use.

Version of LiveCode used: 9.0.0 - Build 15017

~ End~

Tested with LiveCode 9.0 in Linux Mint 18 (Sarah)