Field input of MAC address: filtered and nice to look

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

trevix
Posts: 1087
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Field input of MAC address: filtered and nice to look

Post by trevix » Tue Aug 26, 2025 10:33 am

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
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10127
Joined: Fri Feb 19, 2010 10:17 am

Re: Field input of MAC address: filtered and nice to look

Post by richmond62 » Tue Aug 26, 2025 12:40 pm

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.
-
Screenshot 2025-08-26 at 14.35.33.png
Attachments
Get Mac Address.livecode.zip
Stack.
(1.75 KiB) Downloaded 17 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Tue Aug 26, 2025 2:14 pm

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

stam
Posts: 3105
Joined: Sun Jun 04, 2006 9:39 pm

Re: Field input of MAC address: filtered and nice to look

Post by stam » Tue Aug 26, 2025 3:50 pm

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.
Attachments
MAC address.livecode.zip
(1.38 KiB) Downloaded 16 times

trevix
Posts: 1087
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Field input of MAC address: filtered and nice to look

Post by trevix » Tue Aug 26, 2025 4:24 pm

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.
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

stam
Posts: 3105
Joined: Sun Jun 04, 2006 9:39 pm

Re: Field input of MAC address: filtered and nice to look

Post by stam » Tue Aug 26, 2025 4:29 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Tue Aug 26, 2025 6:04 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10127
Joined: Fri Feb 19, 2010 10:17 am

Re: Field input of MAC address: filtered and nice to look

Post by richmond62 » Tue Aug 26, 2025 6:44 pm

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Tue Aug 26, 2025 7:25 pm

Maybe the user is hankering for a burger?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Tue Aug 26, 2025 9:30 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Tue Aug 26, 2025 9:42 pm

Here is a demo stack. Everything is in the field script. Should work...

Craig
MAC Adresser.livecode.zip
(1.22 KiB) Downloaded 19 times

trevix
Posts: 1087
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Field input of MAC address: filtered and nice to look

Post by trevix » Wed Aug 27, 2025 8:37 am

@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.
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Wed Aug 27, 2025 2:41 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Wed Aug 27, 2025 5:14 pm

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
MAC Addresser2.livecode.zip
(1.31 KiB) Downloaded 16 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Field input of MAC address: filtered and nice to look

Post by dunbarx » Wed Aug 27, 2025 7:03 pm

OK.

My latest offering does not handle the user selecting several chars in an existing string.

You need better users.

Working on it...

Craig

Post Reply