• Here I've been collecting the simple solutions under Power Builder environmental scripts and controls. Take a glance at these questions and
answers. May be it will be useful for you...
Note: You can learn more about PowerBuilder at Sybase WEB Site.
» Send keyboard events into any valid window
At first declare a function as global or local external. After that call it twice under event script in your control receiving the keys:
// External Function Declaration
Subroutine keybd_event (int bVk, int bScan, int dwFlags, int dwExtraInfo) Library 'user32'
// Script
Constant Long VK_TAB = 9
Constant Long VK_ESCAPE = 27
...
At first declare some functions as global or local external and appropriate structures in newly created non-visual object.
// External Function Declaration
Function long CreateFile(string lpszName, long fdwAccess, long fdwShareMode, long lpsa, long fdwCreate, long fdwAttrsAndFlags, long hTemplateFile ) Library "KERNEL32" Alias For CreateFileA
Function boolean ReadFile(long hFile, ref char lpBuffer[], long nNumberOfBytesToWrite, ref long lpNumberOfBytesWritten, long lpOverlapped ) Library "KERNEL32"
Function boolean WriteFile(long hFile, ref char lpBuffer[], long nNumberOfBytesToWrite, ref long lpNumberOfBytesWritten, long lpOverlapped ) Library "KERNEL32"
Function boolean CloseHandle(long hObject ) Library "KERNEL32"
Function boolean SetCommState(long hFile, ref st_dcb lpDCB) Library "KERNEL32"
Function boolean GetCommState(long hFile, ref st_dcb lpDCB) Library "KERNEL32"
Function boolean SetCommTimeouts(long hFile, ref st_commtimeouts lpCommTimeouts) Library "KERNEL32"
Function boolean GetCommTimeouts(long hFile, ref st_commtimeouts lpCommTimeouts) Library "KERNEL32"
Function boolean SetupComm(long hFile, long dwInQueue, long dwOutQueue) Library "KERNEL32"
Subroutine Sleep(long dwMilliseconds) Library "KERNEL32"
Function long GetLastError() Library "KERNEL32"
// Declaration of Related Structures
type st_commtimeouts from structure
unsignedlong readintervaltimeout
unsignedlong readtotaltimeoutmultiplier
unsignedlong readtotaltimeoutconstant
unsignedlong writetotaltimeoutmultiplier
unsignedlong writetotaltimeoutconstant
end type
type st_dcb from structure
unsignedlong dcblength
unsignedlong baudrate
unsignedlong fdummy2
integer wreserved
integer xonlim
integer xofflim
character bytesize
character parity
character stopbits
character xonchar
character xoffchar
character errorchar
character eofchar
character evtchar
integer wreserved1
end type
// Declaration of Instance Variables
st_dcb lpDCB
st_commtimeouts lpCTO
Long Handle = 0
Next we're going to create high-level functions to work with. This three functions (uf_openport(), uf_writeport(), uf_closeport()) have all to deal
with ports:
// open the port by creating the file
Handle = CreateFile(as_portname, GENERIC_READ + GENERIC_WRITE, 0, lNull, OPEN_EXISTING, 0, lNull)
If Handle >= 0 Then
If GetCommState(Handle, lpDCB) Then
//BaudRate - 2400,4800,9600,19400,etc.
lpDCB.BaudRate = Long(9600)
//ByteSize(Data Bits) - 4,5,6,7,8
lpDCB.ByteSize = Char(8)
//Parity - 0=None;1=Odd;2=Even;3=Mark;4=Space
lpDCB.Parity = Char(0)
//StopBits - 0=1;1=1.5;2=2
lpDCB.StopBits = Char(0)
If Not SetCommState(Handle, lpDCB) Then
CloseHandle(Handle)
RETURN FALSE
End If
Else
CloseHandle(Handle)
RETURN FALSE
End If
If GetCommTimeouts(Handle, lpCTO) Then
//Set up following variables to values you want
lpCTO.ReadIntervalTimeout = Long(50)
lpCTO.ReadTotalTimeoutMultiplier = Long(0)
lpCTO.ReadTotalTimeoutConstant = Long(1000)
lpCTO.WriteTotalTimeoutMultiplier = Long(0)
lpCTO.WriteTotalTimeoutConstant = Long(0)
If Not SetCommTimeouts(Handle, lpCTO) Then
CloseHandle(Handle)
RETURN FALSE
End If
SetupComm(Handle, Long(1024), Long(1024))
Else
CloseHandle(Handle)
RETURN FALSE
End If
Else
RETURN FALSE
End If
RETURN TRUE
end function
function long uf_writeport (string as_string);
// RETURN VALUES (Example):
// 128 - ERROR
// 131 - SUCCESSFUL
// 100 - PORT DELAY ERROR
Char lch_input[], lch_output[]
Long ll_written = 0, lNull, ll_counter
If Handle <= 0 Then RETURN 100
SetNull(lNull)
ll_counter = Len(as_string)
Do While ll_counter > 0
lch_input[ll_counter] = Char(Mid(as_string, ll_counter, 1))
ll_counter --
Loop
Sleep(lpcto.ReadIntervalTimeout)
If Not WriteFile(Handle, lch_input, UpperBound(lch_input), ll_written, lNull) Then
RETURN 128 // Write to Port Failed
End If