	section	lard,code

	;how to read RAWKEYS from a screen and de-code using current
	;KEYMAP

	;by shagratt/LSD

	opt	d+
	
code:	IncDir	"DH0:Include/"
	Include	Intuition/Intuition.I
	Include	Intuition/Intuition_Lib.I
	Include	Libraries/dos_LIb.i
	Include	Libraries/Dos.i
	Include	Exec/Exec_Lib.I
	Include	Exec/Exec.I
	Include	Graphics/Graphics_Lib.I


	;Include	Misc/EasyStart.i

	Lea	DosName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l 	D0,_DOSBase
	Beq	Error

	Lea	GFXName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l 	D0,_GFXBase
	Beq	Error

	Lea	IntName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l	D0,_IntuitionBase
	Beq	Error

	*-----------------------------------------------*

	move.l	#0,a1
	CALLEXEC	FindTask

	lea	reply_port,a1
	move.l	d0,MP_SIGTASK(a1)
	CALLEXEC	AddPort
		
	lea	IO_port,a1
	move.l	#-1,d0
	move.l	#0,d1
	lea	console_dev,a0
	CALLEXEC	OpenDevice
	cmp.l	#0,d0
	bne	Quit
	
	lea	IO_port,a0
	move.l	IO_DEVICE(a0),d0
	move.l	d0,ConsoleDevice

	*-----------------------------------------------*

	Lea	MyWindow,a0
	CALLINT	OpenWindow
	Move.l	D0,_MyWinBase
	Beq	CloseInt
	Move.l	D0,A0
	Move.l	wd_RPort(A0),_MyWinRPort
	Move.l	wd_UserPort(a0),_MyWinUserPort

	*-----------------------------------------------*

WaitLoop	Move.l	_MyWinUserPort,A0
	CALLEXEC	WaitPort
	Move.l	_MyWinUserPort,A0
	CALLEXEC	GetMsg
	
	Move.l	D0,A1
	Move.l	im_Class(A1),D2	;D2=IDCMP Flags Directly
	
	Cmp.l	#RAWKEY,D2
	bne	.next

	Movem.l	D0-D7/A0-A6,-(A7)	
	
	lea	Event,a0
	move.b	#IECLASS_RAWKEY,ie_Class(a0)
	move.w	im_Code(a1),ie_Code(a0)
	move.w	im_Qualifier(a1),ie_Qualifier(a0)
	move.l	im_IAddress,ie_EventAddress(a0)

	lea	ConBuff,a1
 
	move.l	#15,d1
	move.l	#0,a2
	move.l	ConsoleDevice,a6
	jsr	-48(a6)
	cmp.l	#0,d0
	beq	.nokeys

	lea	Ascii,a0
	lea	ConBuff,a1
.copy	move.b	(a1),(a0)+
	move.b	#0,(a1)+
	cmp.b	#0,(a1)
	bne	.copy

	move.w	$dff006,$dff180

.nokeys	Movem.l	(a7)+,D0-D7/A0-A6
	
.next	Move.l	D0,A1
	Move.l	im_Class(A1),D2	;D2=IDCMP Flags Directly
	Move.w	im_Code(A1),D3	;D3=Data ie key why class=Rawkey
	Move.w	im_Qualifier(A1),D4	;D4=things like CTRL SHIFT
	Move.w	im_MouseX(a1),D5	;D5=MouseX Coordinate
	Move.w	im_MouseY(a1),D6	;D5=MouseY Coordinate
	Move.l	im_IAddress(A1),D7	;D7=Addres of Intuition Obj ie Gadget
	Movem.l	D0-D7/A0-A6,-(A7)	
	CALLEXEC	ReplyMsg
	Movem.l	(a7)+,D0-D7/A0-A6

	Cmp.l	#CLOSEWINDOW,D2
	bne	WaitLoop

Exit	Move.l	_MyWinBase,A0
	CALLINT	CloseWindow

Quit	lea	reply_port,a1
	CALLEXEC	RemPort
	
	lea	IO_port,a1
	CALLEXEC	CloseDevice


CloseInt	Move.l	_IntuitionBase,A1
	CALLEXEC	CloseLibrary

	Move.l	_DOSBase,A1
	CALLEXEC	CloseLibrary

	Move.l	_GFXBase,A1
	CALLEXEC	CloseLibrary

	*-------------------------------*

Error	Moveq	#0,D0
	Rts

	*-------------------------------*


_MyScrBase		Dc.l	0
_MyWinBase		Dc.l	0
_MyWinRPort		Dc.l	0
_MyWinUserPort	Dc.l	0
_MyWinVPort		Dc.l	0

IO_port:		dcb.l	20,0
reply_port:		dcb.l	8,0
ConsoleDevice:	dc.l	0

ConBuff:		ds.b	16
Event:		ds.b	16
Ascii:		dc.l	0,0,0

console_dev		dc.b	"console.device",0

_IntuitionBase	Dc.l	0
_DOSBase		Dc.l	0
_GFXBase		dc.l	0
IntName		INTNAME
DosName		DOSNAME
GFXName		GRAFNAME
		even

**************************************************

MyWindow:	dc.w	0,0
	dc.w	640,200
	dc.b	0,1
	dc.l	CLOSEWINDOW+RAWKEY
	dc.l	WINDOWSIZING+WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+RMBTRAP+NOCAREREFRESH
	dc.l	0
	dc.l	0
	dc.l	NewWindowName1
	dc.l	0
	dc.l	0
	dc.w	5,5
	dc.w	-1,-1
	dc.w	WBENCHSCREEN
NewWindowName1:
	dc.b	'Your new window',0
	cnop 0,2


; end of PowerWindows source generation
	

