In an earlier post I wrote about prompting commands in a CL program, and allow you to change some of the command's parameters. But I did not give the logic on how to cope if the F3, Exit, or F12, Cancel, keys were pressed.

How to differentiate between the F3 and F12 keys being pressed was a subject of a discussion in one of the Facebook groups I am a member of. As I should have mention this in my earlier post I thought I would give an example of how I would do this.

The person asking the question was prompting the Display User Profile command, DSPUSRPRF , and need to:

If Enter pressed execute the command.

If F3 was pressed exit the program.

If F12 was pressed redisplay the DSPUSRPRF command.

When the F3 or F12 is pressed for all the commands I am aware of the message CPF6801 is sent to the program, along with a string containing the function key. So how can one tell which of the two command keys was used?

Fortunately the Monitor Message command, MONMSG has the Comparison data parameter, CMPDTA , which is used to compare an entered value to the string returned by the message.

So let's go straight to my example program:

01 PGM 02 DCL VAR(&LOOP) TYPE(*LGL) VALUE('1') 03 DOWHILE COND(&LOOP) 04 ?DSPUSRPRF 05 MONMSG MSGID(CPF6801) CMPDTA(F3) EXEC(DO) 06 SNDPGMMSG MSG('F3 pressed') TOPGMQ(*EXT) 07 RETURN 08 ENDDO 09 MONMSG MSGID(CPF6801) CMPDTA(F12) EXEC(DO) 10 SNDPGMMSG MSG('F12 pressed') TOPGMQ(*EXT) 11 ITERATE 12 ENDDO 13 ENDDO 14 ENDPGM

Line 2 and 3: I am going to use a Do loop rather than a Goto. If you are unfamiliar with the Do in CL you need to read CL does do.

Line 4: The question mark ( ? ) immediately before the DSPUSRPRF command will prompt the command on the screen.

If Enter is pressed the DSPUSRPRF command is executed.

Lines 5 – 8: If F3 is pressed then the string " F3 " is in the string returned to the program along with the CPF6801 . By having F3 in the CMPDTA parameter of the MONMSG command the code on lines 6 and 7 are only executed if the F3 has been pressed. Normally I would not have a line, line 6, informing the user that they had pressed F3, but this is an example program. On line 8 I could have used to LEAVE command to exit the Do loop, but the RETURN means I exist the program at that point.

Lines 9 – 12: The logic for the F12 is very similar to that for the F3, the only difference being that I am checking for " F12 " in the CMPDTA parameter. As before I would not send a message to the user informing them that they has pressed F12, line 10, but this is an example program. On line 11 I have used the ITERATE command, in this example it is unnecessary, but in another there might be logic that is executed after the DSPUSRPRF that I would not want executed if F12 had been pressed.

Yes it is really that simple.

I would normally make this more complicated for my own use. If I wanted to output the basic information for a user profile to an output file I would change line 4 to be:

04 ?DSPUSRPRF ?*TYPE(*BASIC) + ?*OUTPUT(*OUTFILE) + ?*OUTFILE(QTEMP/DSPUSRPRF) + ?*OUTMBR(*FIRST *ADD)

The " ?* " mean that the parameter will be displayed when promoted, but cannot be changed. Only the User profile parameter in the command can be entered, all of the others are display only.

You can learn more about the MONMSG command from the IBM website here.

This article was written for IBM i 7.2, and should work for earlier releases too.