Page 1 of 2
Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 10:33 am
by trevix
I want to present the mobile user an input field where he can write a MAC address (12 characters).
I would like to make it easy for the user, having filtered input, all capital, ":" in between each couple of chars, so that on screen the final content will look with something like this "AC:23:3F:AE:2C:B0".
I have put together some kind of code that works, but it is not as clean as the task suggests.
If you have a better method, I would like to hear about it.
Code: Select all
on keyDown pKeyName
lock screen
put the text of me & toupper(pKeyName) into tText
if pKeyName is not in "0123456789ABCDEF" then
exit keyDown
end if
put the number of chars of tText into tNum --Max 12 + ":"
put tNum & cr after msg
if tNum >= 18 then
exit keyDown
end if
if tNum >= 0 then
set the visible of fld "LabelHide" of the owner of me to false
replace ":" with empty in tText
put the number of chars of tText into tNum
repeat with U = 1 to tNum
put char U of tText after tText2
if U mod 2 = 0 AND U < 12 then put ":" after tText2
end repeat
set the text of me to tText2
select after char -1 of me
else
set the visible of fld "LabelHide" of the owner of me to true
end if
end keyDown
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 12:40 pm
by richmond62
I have 'stolen' some code from here:
viewtopic.php?t=14081
https://www.sonsothunder.com/devres/liv ... env001.htm
and modified it a very small bit (mainly in changing it from a
function to a
command).
This will NOT allow an end-user enter a Mac address, but it will tell someone WHAT the Mac address of their machine is.
I would not doubt that a bit of further 'clever work' could extend this to work for mobile devices.
-
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 2:14 pm
by dunbarx
Trevix.
HI.
Try this in a fld script:
Code: Select all
on keyDown pKeyName
if the number of chars of me = 17 then exit keydown
if pKeyName is not in "0123456789ABCDEF" then exit keyDown
put toUpper(pKeyName) after me
if (the number of chars of me - 2) mod 3 = 0 then put ":" after me
if the number of chars of me = 18 then delete the last char of me
end keyDown
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 3:50 pm
by stam
dunbarx wrote: ↑Tue Aug 26, 2025 2:14 pm
Code: Select all
on keyDown pKeyName
if the number of chars of me = 17 then exit keydown
if pKeyName is not in "0123456789ABCDEF" then exit keyDown
put toUpper(pKeyName) after me
if (the number of chars of me - 2) mod 3 = 0 then put ":" after me
if the number of chars of me = 18 then delete the last char of me
end keyDown
yeah that works...
unless you delete mid-string.
If you have the text
AB:CD:EF and delete "E" and re-type it, you end up with
AB:CD:F:E
Controlling the delete/backspace keys means you have to use rawKeyDown.
It's a bit more of a pain to allow that, but it's more natural for the user to go and delete something mid-string.
My version of the solution uses a script-local flag to track if forward-delete or backspace is pressed (to track where selection/caret should go), a function to insert a ":" every 2 chars triggered by he textChanged handler which also repositions the selection/caret where text input should go, and uses the rawKeyDown handler to allow only the permitted chars to be entered into the field up to total of 17 chars.
textChanged (note: this uses a script local flag which is set in rawKeyDown and used in textChanged)
Code: Select all
local sDeleteFlag = false
on textChanged
local tSelection
if sDeleteFlag then
put word 4 of the selectedChunk of me into tSelection
else
put word 2 of the selectedChunk of me into tSelection
end if
put getTextAsMAC(the text of me) into me
select after char tSelection of me
end textChanged
function for ":" insertion and convert to upper case
Code: Select all
function getTextAsMAC pText
local tResult
replace ":" with empty in pText
set the itemDelimiter to ":"
repeat with x = 2 to the number of chars of pText step 2
put char x-1 of pText & char x of pText into item (the number of items of tResult + 1) of tResult
end repeat
return toUpper(tResult)
end getTextAsMAC
rawKeyDown handler - only allows permitted chars and up to a max length of 17 chars including the ":"s
Code: Select all
on rawKeyDown pKeyCode
switch
case pKeyCode > 64 and pKeyCode < 71 and length(the text of me) < 17 # upper case
case pKeyCode > 96 and pKeyCode < 103 and length(the text of me) < 17 # lower case
case pkeyCode > 47 and pKeyCode < 58 and length(the text of me) < 17 # numbers
case pKeyCode > 65360 and pKeyCode < 65365 # arrow keys
put false into sDeleteFlag
pass rawKeyDown
break
case pKeyCode = 65535 or pkeyCode = 65288 # delete/backspace
put true into sDeleteFlag
pass rawKeyDown
break
end switch
end rawKeyDown
If you want to allow deletion mid-string this is a more verbose way of letting it happen.
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 4:24 pm
by trevix
So clever Craig!! Too bad it still need a fix.
I also noticed that if I delete the last ":" (like from "AA:BB:" to "AA:BB", because the user entered a wrong char), I don't get back the ":" with the next char, but I get "AA:BBC". (fixed afterward...but that's not the point.
What mentioned by Stam is another problem: being on mobile, I wander if the position of the insertion point can be detected.
This would allow probably an easier way to do it, but a repeat routine is probably needed to place the ":" in the correct places.
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 4:29 pm
by stam
trevix wrote: ↑Tue Aug 26, 2025 4:24 pm
...being on mobile, I wander if the position of the insertion point can be detected.
This would allow probably an easier way to do it, but a repeat routine is probably needed to place the ":" in the correct places.
Both
textChanged,
rawKeyDown and
selectedChunk (used for the selection/caret position) are available for mobile, so in principle you should be able to implement something similar to my solution, but I have no idea how these may work with mobile controls...
As you mention, you constantly need to reposition the ":", so an additional handler/function is needed, and this really can only be triggered by textChanged (or a button I guess), as keyDown won't trigger with delete keys, and rawKeyDown has to pass the event to the field for changes to appear there, so is only really useful for filtering if you want to trap deletion.
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 6:04 pm
by dunbarx
All.
I think all this can be smaller:
Code: Select all
on keyDown pKeyName
if the number of chars of me = 17 then exit keydown
if pKeyName is not in "0123456789ABCDEF" then exit keyDown
set the itemDel to ":"
if the number of chars of last item of me = 2 and the last char of me <> ":" then put ":" after me
put toUpper(pKeyName) after me
if (the number of chars of me - 2) mod 3 = 0 then put ":" after me
if the number of chars of me = 18 then delete the last char of me
end keyDown
Now any action of the user seems to work.
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 6:44 pm
by richmond62
I want to present the mobile user an input field where he can write a MAC address (12 characters).
I am extremely curious to know which MAC address your user would want to write in a field apart from the one of the device on which your stack is running?
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 7:25 pm
by dunbarx
Maybe the user is hankering for a burger?
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 9:30 pm
by dunbarx
I am concerned about how much I really know about things.
I rarely use the forward delete key, in fact never except in "Excel". In an editable field in LC, pressing the backSpace key loses the last char in an active field. I thought this was what forward delete also was designed to do, and so says the dictionary. So in my handler above, in order to cover anything a user might do, I have to add:
Code: Select all
on deleteKey
delete the last char of me
end deleteKey
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Tue Aug 26, 2025 9:42 pm
by dunbarx
Here is a demo stack. Everything is in the field script. Should work...
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Wed Aug 27, 2025 8:37 am
by trevix
@dunbarx:
not quite.
Type "AB" > "AB:"
Insert the pointer between A and B (in order do modify A)
backspace delete A
type A again (to obtain AB:) > "B:A" (wrong)
click outside
click inside the field > everything disappear (wrong)
@richmond62:
It is used to authorize, in the app, a BLE button, using its Mac address, so that its iBeacon messages are triggering something in the app.
Re: Field input of MAC address: filtered and nice to look
Posted: Wed Aug 27, 2025 2:41 pm
by dunbarx
Trevis.
I left the "mouseEnter" handler to clear the field since it made development easier. That should be deleted.
As for inserting a char in the middle of an existing string, who knew? I guess this is why the programmer should always ask for independent testing.
OK, working on it.
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Wed Aug 27, 2025 5:14 pm
by dunbarx
Trevis.
OK, this time, definitely. Er, maybe. Still keeping to my minimalist text wrangler handler:
Code: Select all
on keyDown pKeyName
get word 2 of the selectedChunk
if the number of chars of me = 17 then exit keydown
if pKeyName is not in "0123456789ABCDEF" then exit keyDown
if it <> the number of chars of me + 1 then -- Deal with internal manual editing
put toUpper(pKeyName) after char it - 1 of me
if the last char of me <> ":" then put ":" after me
select after me
exit to top
end if
set the itemDel to ":"
if the number of chars of last item of me = 2 and the last char of me <> ":" then put ":" after me
put toUpper(pKeyName) after me
if (the number of chars of me - 2) mod 3 = 0 then put ":" after me
if the number of chars of me = 18 then delete the last char of me
end keyDown
Craig
Re: Field input of MAC address: filtered and nice to look
Posted: Wed Aug 27, 2025 7:03 pm
by dunbarx
OK.
My latest offering does not handle the user selecting several chars in an existing string.
You need better users.
Working on it...
Craig