Recording user actions

Some applications are better served when you record the user's actions. In this context, a user action refers to an in-app behavior such as clicking on/touching a button or moving a slider. In this recipe, we will create a user action log and program a button to record user actions.

How to do it...

To record user actions, perform the following steps:

  1. Create a new main stack for a mobile application.
  2. Add three buttons across the top named Safe, Secure, and Restricted.
  3. Add a button named Reset Log at the bottom-center of the card.
  4. Create a scrolling text field named fldLog to fill the remainder of the card.
  5. Set the background color of the fldLog field to black.
  6. Set the foreground color of the fldLog field to yellow. This will be the color of the text entered into the log.
  7. Set text size of the fldLog field to 14.
  8. Set the traversalOn property to false (deselect the Focusable checkbox in the Basic Properties section of the property inspector).
  9. When you complete steps 1 to 5, your interface should look similar to the following screenshot. Make any adjustments to your four buttons and the scrolling text field before moving on to step 7.
  10. Add the following code to the card that contains your interface:
    global logLine
    
    on preCardOpen
      put 1 into logLine
    end preCardOpen
    
    on updateLog msg2log
      put msg2log into line logLine of fld "fldLog"
      add 1 to logLine
    end updateLog
  11. Add the following code to the Reset Log button:
    on mouseUp
      global logLine
      
      put 1 into logLine
      put empty into fld "fldLog"
    end mouseUp
  12. Add the following code to the Safe, Secure, and Restricted buttons:
    on mouseUp
      updateLog(short name of me & ": mouseUp")
    end mouseUp
    
    on mouseDown
      updateLog(short name of me & ": mouseDown")
    end mouseDown
    
    on mouseEnter
      updateLog(short name of me & ": mouseEnter")
    end mouseEnter
    
    on mouseLeave
      updateLog(short name of me & ": mouseLeave")
    end mouseLeave
  13. Test your application. It should look similar to the following screenshot:

How it works...

By creating listeners (also known as handlers), we can record user actions in our apps. Depending upon your application, you might want to create a system log for security purposes or even to provide a display (as we did in our example) for users to review.