Here is a tool I found useful to display the events received by a CanvasGadget.
It's a visual way to see what is received by the CanvasGadget, when it is received, and how it changes as the gadget is interacted with.
I tried to make it as self-explanatory as possible. For the maximum benefit it should be used hand-in-hand with the PB Manual to understand in more detail what is being displayed.
Code: Select all
;Author: Demivec
;program: displays CanvasGadget events in a graphical way
;Written for PB v4.61
;
;Note: All events are displayed. All attributes are displayed except
; those dealing with the mouse cursor image, the gadget image, and
; mouse cliping status.
;
; This program was written for a MicroSoft Windows system. It should
;also function well for Linux. Some mild attempts have been made to
;account for differences that occur when running on a Mac. These attempts
;detected the Command Key instead of the Control Key and also react to a
;MouseLeave event by recording all the mouse buttons as being released.
;
;My appologies for the colors chosen for the display. I was having a bad day ;).
EnableExplicit
Procedure displayCanvasEvents(gadgetID = 0)
Static kx = -40, ky = 5, mx = 5, my = 5, vx = 310, vy = 45, lx = 95, ly = 300
Static is_L_ButtonDown, is_M_ButtonDown, is_R_ButtonDown
Static hasKeyboardFocus, isMouseOver
Static Dim isVKeysDown(255)
Protected event = EventType(), px, py, buttonStatus, keyStatus, rawKeyCode
px = GetGadgetAttribute(gadgetID, #PB_Canvas_MouseX): py = GetGadgetAttribute(gadgetID, #PB_Canvas_MouseY)
buttonStatus = GetGadgetAttribute(gadgetID, #PB_Canvas_Buttons)
keyStatus = GetGadgetAttribute(gadgetID, #PB_Canvas_Modifiers)
rawKeyCode = GetGadgetAttribute(gadgetID, #PB_Canvas_Key)
Protected move_fcolor = $FFFFFF, wheel_fcolor = $FFFFFF, input_fcolor = $FFFFFF, raw_fcolor = $FFFFFF
Select event
Case #PB_EventType_MouseEnter: isMouseOver = 1
Case #PB_EventType_MouseLeave: isMouseOver = 0
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
;signal mousebutton release if mouse leaves gadget
is_L_ButtonDown = 0
is_M_ButtonDown = 0
is_R_ButtonDown = 0
CompilerEndIf
Case #PB_EventType_Focus : hasKeyboardFocus = 1: Dim isVKeysDown(255)
Case #PB_EventType_LostFocus: hasKeyboardFocus = 0
Case #PB_EventType_LeftButtonDown: is_L_ButtonDown = 1
Case #PB_EventType_LeftButtonUp: is_L_ButtonDown = 0
Case #PB_EventType_MiddleButtonDown: is_M_ButtonDown = 1
Case #PB_EventType_MiddleButtonUp: is_M_ButtonDown = 0
Case #PB_EventType_RightButtonDown: is_R_ButtonDown = 1
Case #PB_EventType_RightButtonUp: is_R_ButtonDown = 0
Case #PB_EventType_MouseMove: move_fcolor = $D402B4
Case #PB_EventType_MouseWheel: wheel_fcolor = $D402B4
Case #PB_EventType_Input: input_fcolor = $D402B4
Case #PB_EventType_KeyDown: raw_fcolor = $D402B4: isVKeysDown(rawKeyCode) = 1
Case #PB_EventType_KeyUp: raw_fcolor = $D402B4: isVKeysDown(rawKeyCode) = 0
EndSelect
Protected x, i
StartDrawing(CanvasOutput(gadgetID))
Box(0, 0, OutputWidth(), OutputHeight(), $C0E0C0)
;legend
DrawingMode(#PB_2DDrawing_Transparent)
x = DrawText(lx + 0, ly, "Legend", 0)
x = DrawText(x, ly, ": ", $FFFFFF)
x = DrawText(x, ly, "Last Message", $D402B4)
x = DrawText(x, ly, ", ", $FFFFFF)
DrawingMode(#PB_2DDrawing_Default)
x = DrawText(x, ly, "Historical Status", $FFFFFF ! $2020, $C0E0C0 ! $2020)
x = DrawText(x, ly, ", ", $FFFFFF, $C0E0C0)
x = DrawText(x, ly, "Active Status", 0, $C0E0C0 ! $2020)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(mx + 58, my + 0, "Mouse", 0)
x = DrawText(mx + 7, my + 90, "Move :", move_fcolor): DrawText(x + 5, my + 90, RSet(Str(px), 5, " ") + ", " + RSet(Str(py), 5, " "))
x = DrawText(mx + 5, my + 110, "Wheel:", wheel_fcolor): DrawText(x + 5, my + 110, RSet(Str(GetGadgetAttribute(0, #PB_Canvas_WheelDelta)), 2, " "))
;mouse over
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
Box(mx + 55, my + 30, 50, 20): DrawText(mx + 63, my + 31, "Enter")
Box(mx + 55, my + 50, 50, 20): DrawText(mx + 67, my + 51, "Exit")
DrawingMode(#PB_2DDrawing_XOr)
If event = #PB_EventType_MouseEnter Or event = #PB_EventType_MouseLeave
Box(mx + 55, my + 50 - 20 * isMouseOver, 50, 20, $14E200)
Else
Box(mx + 55, my + 50 - 20 * isMouseOver, 50, 20, $2020)
EndIf
;mouse buttons
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
DrawText(mx + 55, my + 150, "Buttons", 0)
Box(mx + 50, my + 170, 20 * 3, 20 * 5): DrawText(mx + 30, my + 172, "CC")
Box(mx + 50, my + 190, 20 * 3, 20 * 3): DrawText(mx + 38, my + 192, "C")
Box(mx + 50, my + 210, 20 * 3, 20 * 1): DrawText(mx + 20, my + 212, "Msg")
Box(mx + 70, my + 170, 20 * 1, 20 * 5)
DrawText(mx + 55, my + 212, "L", 0): DrawText(mx + 74, my + 212, "M", 0): DrawText(mx + 94, my + 212, "R", 0)
DrawText(mx + 38, my + 232, "U")
DrawText(mx + 38, my + 252, "D")
DrawingMode(#PB_2DDrawing_XOr)
;Test for is_L_ButtonDown is to differentiate between no event (=0) and an actual LeftClick event (=0).
;This allows this procedure to draw it's output even when no actual gadget events are being interpreted.
If event = #PB_EventType_LeftClick And is_L_ButtonDown: Box(mx + 50, my + 190, 20, 20, $14E274): EndIf
If event = #PB_EventType_RightClick: Box(mx + 90, my + 190, 20, 20, $14E274): EndIf
If event = #PB_EventType_LeftDoubleClick: Box(mx + 50, my + 170, 20, 20, $14E274): EndIf
If event = #PB_EventType_RightDoubleClick: Box(mx + 90, my + 170, 20, 20, $14E274): EndIf
If event = #PB_EventType_LeftButtonUp Or event = #PB_EventType_LeftButtonDown
Box(mx + 50, my + 230 + 20 * is_L_ButtonDown, 20, 20, $14E274)
Else
Box(mx + 50, my + 230 + 20 * is_L_ButtonDown, 20, 20, $2020)
EndIf
If event = #PB_EventType_MiddleButtonUp Or event = #PB_EventType_MiddleButtonDown
Box(mx + 70, my + 230 + 20 * is_M_ButtonDown, 20, 20, $14E274)
Else
Box(mx + 70, my + 230 + 20 * is_M_ButtonDown, 20, 20, $2020)
EndIf
If event = #PB_EventType_RightButtonUp Or event = #PB_EventType_RightButtonDown
Box(mx + 90, my + 230 + 20 * is_R_ButtonDown, 20, 20, $14E274)
Else
Box(mx + 90, my + 230 + 20 * is_R_ButtonDown, 20, 20, $2020)
EndIf
If buttonStatus & #PB_Canvas_LeftButton > 0 : Box(mx + 50, my + 210, 20, 20, $2020): EndIf
If buttonStatus & #PB_Canvas_MiddleButton > 0: Box(mx + 70, my + 210, 20, 20, $2020): EndIf
If buttonStatus & #PB_Canvas_RightButton > 0 : Box(mx + 90, my + 210, 20, 20, $2020): EndIf
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(kx + 250, ky + 0, "Key", 0)
;keyboard focus
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
Box(kx + 220, ky + 30, 86, 20): DrawText(kx + 243, ky + 31, "Focus")
Box(kx + 220, ky + 50, 86, 20): DrawText(kx + 228, ky + 51, "Lost Focus")
DrawingMode(#PB_2DDrawing_XOr)
If event = #PB_EventType_Focus Or event = #PB_EventType_LostFocus
Box(kx + 220, ky + 50 - 20 * hasKeyboardFocus, 86, 20, $14E200)
Else
Box(kx + 220, ky + 50 - 20 * hasKeyboardFocus, 86, 20, $2020)
EndIf
;key codes
DrawingMode(#PB_2DDrawing_Transparent)
x = DrawText(kx + 210, ky + 90, "Raw Code: ", raw_fcolor): DrawText(x, ky + 90, "$" + RSet(Hex(rawKeyCode), 2, "0"))
x = DrawText(kx + 206, ky + 110, "Input Code: ", input_fcolor): DrawText(x, ky + 110, "$" + RSet(Hex(GetGadgetAttribute(gadgetID, #PB_Canvas_Input)), 2, "0"))
;key modifiers
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
DrawText(kx + 232, ky + 190, "Modifiers", 0)
Box(kx + 232, ky + 210, 20 * 3, 20 * 1)
Box(kx + 252, ky + 210, 20 * 1, 20 * 1)
DrawText(kx + 238, ky + 212, "S", 0): DrawText(kx + 258, ky + 212, "A", 0): DrawText(kx + 277, ky + 212, "C", 0)
DrawText(kx + 202, ky + 212, "Msg")
DrawingMode(#PB_2DDrawing_XOr)
If keyStatus & #PB_Canvas_Shift > 0 : Box(kx + 232, ky + 210, 20, 20, $2020): EndIf
If keyStatus & #PB_Canvas_Alt > 0: Box(kx + 252, ky + 210, 20, 20, $2020): EndIf
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
If keyStatus & #PB_Canvas_Command > 0 : Box(kx + 272, ky + 210, 20, 20, $2020): EndIf
CompilerElse
If keyStatus & #PB_Canvas_Control > 0 : Box(kx + 272, ky + 210, 20, 20, $2020): EndIf
CompilerEndIf
;virtual keys status
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
DrawText(vx + 50, vy - 40, "Virtual Key Status")
Box(vx + 0, vy + 0, 16 * 14, 16 * 14)
Line(vx + 0, vy + 8 * 14, 16 * 14, 1)
Line(vx + 8 * 14, vy + 0, 1, 16 * 14)
For i = 0 To 15: DrawText(vx + i * 14 + ((14 - TextWidth(Hex(i))) / 2), vy - 16, Hex(i)): Next
For i = 0 To 15: DrawText(vx - 18, vy + i * 14 + ((14 - TextHeight(Hex(i) + "x")) / 2), Hex(i) + "x"): Next
For i = 1 To 7
Box(vx + 0, vy + i * 14, 16 * 14, (8 - i) * 2 * 14)
Box(vx + i * 14, vy + 0, (8 - i) * 2 * 14, 16 * 14)
Next
DrawingMode(#PB_2DDrawing_XOr)
For i = 0 To 255
If isVKeysDown(i) = 1
If rawKeyCode = i
Box(vx + (i % 16) * 14, vy + (i / 16) * 14, 14, 14, $14E274)
Else
Box(vx + (i % 16) * 14, vy + (i / 16) * 14, 14, 14, $2020)
EndIf
EndIf
Next
StopDrawing()
Delay(10) ;slow down display of events
EndProcedure
OpenWindow(0, 0, 0, 550, 350, "Canvas Gadget event reporter", #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 550, 320, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
CanvasGadget(1, 0, 320, 550, 30, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
StartDrawing(CanvasOutput(1))
DrawText((OutputWidth() - TextWidth("Another Gadget")) / 2, (OutputHeight() - TextHeight("Another Gadget")) / 2, "Another Gadget", 0, $FFFFFF)
StopDrawing()
displayCanvasEvents()
Define event
Repeat
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
Break 2
EndIf
If event = #PB_Event_Gadget
If EventGadget() = 0
displayCanvasEvents()
EndIf
EndIf
Until event = 0
ForEver
@Edit: Minor code update to 'Define' a variable. Thanks ts-soft.
@Edit: A major improvement to the code is posted below that allows dynamic monitoring of any canvas gadget and is implemented as an include file.