Highlighted
Absent Member.
Absent Member.
654 views

[archive] Toggle lock keys in Windows environment

[Migrated content. Thread originally posted on 25 June 2003]

As most of us develop software for administrative purposes, it might be convenient to control the "lock" keys of the keyboard.

Here is some source illustrating how to inquire the current status of, and how to toggle the Numlock, Scroll-lock and Capslock keys:

IDENTIFICATION DIVISION.
PROGRAM-ID. SetNumLck.
WORKING-STORAGE SECTION.

77 key-val pic 9(2) comp-5.
77 HStat USAGE HANDLE OF STATUS-BAR.
77 NUM-STYLE PIC 9 VALUE 1.
77 SCR-STYLE PIC 9 VALUE 2.
77 CAP-STYLE PIC 9 VALUE 2.
77 CHOICE PIC X.
77 WS-ESC PIC 9(3) VALUE 0.
88 F1-ESC VALUE 1. | Toggle NUM LOCK
88 F2-ESC VALUE 2. | Toggle SCR LOCK
88 F3-ESC VALUE 3. | Toggle CAP LOCK
88 F4-ESC VALUE 4. | Terminate
88 F5-ESC VALUE 5. | Refresh

78 VK_NUMLOCK VALUE 144.
78 VK_SCROLL VALUE 145.
78 VK_CAPITAL VALUE 20.
78 KEYEVENTF_EXTENDEDKEY VALUE 1.
78 KEYEVENTF_KEYUP VALUE 2.

PROCEDURE DIVISION.
MAIN-LOGIC.

DISPLAY STANDARD GRAPHICAL WINDOW
LINES 10
SIZE 40
BACKGROUND-LOW
TITLE
"Numlock sample".

DISPLAY STATUS-BAR
PANEL-WIDTHS(25, 5, 5, 5)
PANEL-STYLE(0, 0, 0, 0)
PANEL-TEXT("Key status", " ", " ", " ")
HANDLE IN HStat.
SET ENVIRONMENT "DLL-CONVENTION" TO "1".
CALL "USER32.DLL".

MAIN-LOGIC-010.

CALL "GetKeyState" USING
BY VALUE vk_numlock
GIVING key-val.

IF KEY-VAL = 1
MOVE 1 TO NUM-STYLE
ELSE
MOVE 2 TO NUM-STYLE.

CALL "GetKeyState" USING
BY VALUE vk_scroll
GIVING key-val.

IF KEY-VAL = 1
MOVE 1 TO SCR-STYLE
ELSE
MOVE 2 TO SCR-STYLE.

CALL "GetKeyState" USING
BY VALUE vk_capital
GIVING key-val.

IF KEY-VAL = 1
MOVE 1 TO CAP-STYLE
ELSE
MOVE 2 TO CAP-STYLE.

MODIFY HStat
PANEL-STYLE(0, SCR-STYLE, NUM-STYLE, CAP-STYLE)
PANEL-TEXT(" ", "Scr", "Num", "Cap").

MAIN-LOGIC-020.

ACCEPT CHOICE ON EXCEPTION CONTINUE.
ACCEPT WS-ESC FROM ESCAPE KEY.

EVALUATE TRUE
WHEN F1-ESC PERFORM TOGGLE-NUM-LOCK
WHEN F2-ESC PERFORM TOGGLE-SCR-LOCK
WHEN F3-ESC PERFORM TOGGLE-CAP-LOCK
WHEN F4-ESC GO TO MAIN-LOGIC-END
WHEN F5-ESC GO TO MAIN-LOGIC-010
WHEN OTHER GO TO MAIN-LOGIC-020
END-EVALUATE.
GO TO MAIN-LOGIC-010.

MAIN-LOGIC-END.

CANCEL "USER32.DLL".
STOP RUN.

TOGGLE-NUM-LOCK.
*Toggle the current num lock condition
CALL "keybd_event" USING
BY VALUE vk_numlock
BY VALUE 69
BY VALUE keyeventf_extendedkey
BY VALUE 0
END-CALL
CALL "keybd_event" USING
BY VALUE vk_numlock
BY VALUE 69
BY VALUE 3
BY VALUE 0
END-CALL
EXIT PARAGRAPH.

TOGGLE-SCR-LOCK.
*Toggle the current scroll lock condition
CALL "keybd_event" USING
BY VALUE vk_scroll
BY VALUE 69
BY VALUE 1
BY VALUE 0
END-CALL
CALL "keybd_event" USING
BY VALUE vk_scroll
BY VALUE 69
BY VALUE 3
BY VALUE 0
END-CALL
EXIT PARAGRAPH.

TOGGLE-CAP-LOCK.

*Toggle the current caps lock condition
CALL "keybd_event" USING
BY VALUE vk_capital
BY VALUE 69
BY VALUE 1
BY VALUE 0
END-CALL
CALL "keybd_event" USING
BY VALUE vk_capital
BY VALUE 69
BY VALUE 3
BY VALUE 0
END-CALL
EXIT PARAGRAPH.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.