LiveCode: Using the Mouse Wheel with a Single Line Text Field to Increment Year for a Calendar.

Back To LiveCode Main Page

I was working on a calendar program for the fun of it.

I put in a option menu (drops down to reveal the months) and wrote code that when I hover over it and roll my mouse wheel, it will advance the month selection without the need of opening the menu and clicking on the month. You can follow this link How to use the Mouse Wheel with the Option Menu to see how I did that.

In this segment I will be explaining how I did the same for the years. The difference is the year is in a plain single line text field that can accept updates three ways.

  1. Type the year in directly. I wrote code for the field to only accept the numbers 0 to 9 and nothing else. Also I limited the range of the years from 1700 to 2199.

  2. Use the horizontal slider to quickly select the year.

  3. Hover over the year and roll the mouse wheel to adjust the year.

The following code was placed direcly in the text field named CalYear. It is the textbox in image above with the year 2018 in it.

This code is what makes the mouse wheel change the year while hovering over the textbox with the year in it.

on rawKeyDown pKeyCode
-- answer pKeyCode -- Uncomment to test for raw key codes
if pKeyCode is 65309 then -- Test for mouse wheel being rolled upward
if field "CalYear" < 2199 then -- Stop advancing the year at 2199
add 1 to field "CalYear" -- Add another year
set the thumbPosition of scrollbar "SelectYear" to field "CalYear" -- readjust the slider to the new year.
end if
end
if

if
pKeyCode is 65308 then -- Test for mouse wheel being rolled downward
if field "CalYear" > 1700 then -- Stop decreasing the at 1700
subtract 1 from field "CalYear" -- Subtract a year
set the thumbPosition of scrollbar "SelectYear" to field "CalYear" -- readjust the slider to the new year.
end if
end
if

pass rawKeyDown -- Pass raw key codes onward to be used by the system.

end rawKeyDown



To fix the textbox to where it will only accept numbers I used the following code in the field CalYear

on keyDown pKeyName -- Use keyDown to trap the keys before anything else
if pKeyName is among the characters of "0123456789" then -- Limit input to only numbers and nothing else
pass keyDown -- The key input passed the test. This instruction passes the key ownard to be inputed into the text field
else
exit
keyDown -- The key input failed the test. Exit and DO NOT FOWARD the key input to be used.
end if
end
keyDown




The following code limits the year to a range of 1700 to 2199. Even if the user directly typed a year outside that range, the minimum or maximum year will be substituted.

on keyUp pKeyName -- KeyUp traps the keys after the keyDown

if field "CalYear" > 2199 then -- If the year is greater than 2199 then
put 2199 into field "CalYear" -- correct the year to 2199
select after field "CalYear" -- Place the text cursor after the year
end if

if field "CalYear" < 1700 then -- If the year is less than 1700 then
if field "CalYear" > 999 then -- but still is greater than 999 then (This prevents corrections...
-- if the user is in the middle of typing a year in. For example if the user starts to type in the...
-- year 2018 it won't correct at numbers below 1000. Other code blanks out calendar until a...
-- legitimate year is entered.
put 1700 into field "CalYear" -- correct with the year 1700.
select after field "CalYear" -- if a correction is made, then place the text cursor after the year.
end if
end
if

set the thumbPosition of scrollbar "SelectYear" to field "CalYear" -- readjust the slider to the new year.

end keyUp


~ END ~

Note this program was written on a Linux machine and wasn't tested on any other OS.

Version of LiveCode used: 9.0.0 - Build 15017