X
50 Rate this article:
No rating

INTERNAL: Controlling the mouse cursor using the arrow keys

Zachary Norman
Topic:
This tech tip provides an example in which the mouse cursor can be controlled using the keyboard arrow keys.Discussion:
Using a combination of motion events and keyboard events, the mouse cursor can be moved by pressing the keyboard's arrow keys.Solution:
;-----------------------------------------------
;
pro move_mouse_cursor_event, event

    widget_control, event.top, GET_UVALUE = pState
    
    print, event.key

    if event.key eq 0 then begin
    
        (*pState).position = [event.x,event.y]
    
    endif else begin
    
        wset, (*pState).winID
        
        case event.key of
        
            5: (*pState).position = (*pState).position + [-1,0]
            6: (*pState).position = (*pState).position + [1,0]
            7: (*pState).position = (*pState).position + [0,1]
            8: (*pState).position = (*pState).position + [0,-1]
        
            else:
        
        endcase

        tvcrs, (*pState).position[0], (*pState).position[1], /DEVICE
        
    endelse

end


;-----------------------------------------------
;
pro move_mouse_cursor

    tlb = widget_base(COL = 1)
    draw = widget_draw(tlb, XSIZE = 300, YSIZE = 300, $
        KEYBOARD_EVENTS = 2, /MOTION_EVENTS)
        
    widget_control, tlb, /REALIZE
    widget_control, draw, GET_VALUE = winID

    state = {winID:winID, $
             position:[0,0]}
         
    widget_control, tlb, SET_UVALUE = ptr_new(state)

    xmanager, 'move_mouse_cursor', tlb

end