How to detect if the on-screen keyboard is visible

You can use WinAPI function IsWindowVisible.

For Visual Basic, it looks like this:
Dim hWnd As Long 
hWnd = FindWindow("TMainKeyboardForm", vbNullString) 
If (IsWindowVisible(hWnd)) Then ........