At first: welcome to the PureBasic Forum, Eidos...
What do you want to achieve?
If you want to simulate a key press into a StringGadget this can be
done very easily and cross-platform:
Code:
EnableExplicit
OpenWindow(0, 270, 100, 270, 70, "Simulate key press", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
StringGadget(0, 10, 10, WindowWidth(0) - 20, 20, "")
ButtonGadget(1, 30, 40, WindowWidth(0) - 60, 20, "Simulate pressing of <K> key")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 1
If EventType() = #PB_EventType_LeftClick
SetGadgetText(0, GetGadgetText(0) + "K")
EndIf
EndIf
EndSelect
ForEver
If you want to simulate a key press by generating an internal event,
things are becoming more complicated:
Code:
EnableExplicit
ImportC ""
CreateEvent(MemoryAllocatorRef.L, EventClass.L, EventKind.L, EventTime.D, EventAttributes.L, *EventRef)
SendEventToEventTargetWithOptions(EventRef.L, EventTargetRef.L, OptionBits.L)
EndImport
#kEventAttributeNone = 0
#kEventClassControl = 'cntl'
#kEventParamTextInputSendKeyboardEvent = 'tske'
Structure EventTypeSpec
EventClass.L
EventKind.L
EndStructure
ProcedureC ControlHandler(*NextEventHandler, EventRef.L, UserData.L)
Protected Result.L
If GetEventKind_(EventRef) = #kEventParamTextInputSendKeyboardEvent
SetGadgetText(0, GetGadgetText(0) + "K")
EndIf
EndProcedure
Dim EventTypes.EventTypeSpec(0)
Define EventRef.L
Define Result.L
OpenWindow(0, 270, 100, 270, 70, "Simulate key press", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
StringGadget(0, 10, 10, WindowWidth(0) - 20, 20, "")
ButtonGadget(1, 30, 40, WindowWidth(0) - 60, 20, "Simulate pressing of <K> key")
EventTypes(0)\EventClass = #kEventClassControl
EventTypes(0)\EventKind = #kEventParamTextInputSendKeyboardEvent
InstallEventHandler_(GetControlEventTarget_(GadgetID(0)), @ControlHandler(), 1, @EventTypes(), 0, 0)
If CreateEvent(0, #kEventClassControl, #kEventParamTextInputSendKeyboardEvent, 0, #kEventAttributeNone, @EventRef)
Debug "CreateEvent() failed!"
End
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 1
If EventType() = #PB_EventType_LeftClick
SendEventToEventTarget_(EventRef, GetControlEventTarget_(GadgetID(0)))
EndIf
EndIf
EndSelect
ForEver
If you want to know how to detect the pressing of keys (and clicks onto
different mouse buttons) in a window (not as input into a StringGadget)
and display these actions in a TextGadget, then you might take a look
into
this thread.