Scrolling Images with Your Keyboard

In image viewing applications, users often want to be able to scroll an image without their hands leaving the keyboard. Here’s how to extend the RasterMaster imaging SDK scrolling functionality to allow you to scroll from the keyboard:

Your container, which does your image painting, will need to implement the Java KeyListener interface and add the KeyListener in your constructor:

public class SnowFrame extends Frame implements KeyListener

public SnowFrame()




You’ll also need to get handles on the scrollbars from RasterMaster since they are internally maintained. Add these class level members:

private Adjustable hsb = null;

private Adjustable vsb = null;

Finally, you’ll need to implement the three methods required by KeyListener. Although, we’ll only use one of them for this task:

public void keyTyped(KeyEvent ke){}

public void keyReleased(KeyEvent ke){}

public void keyPressed(KeyEvent ke){

if(hsb == null || vsb == null){

if(Simage.vsb != null){

vsb = Simage.vsb.getScrollbar();}

else if(Simage.hsb != null){

hsb = Simage.hsb.getScrollbar();}}

if((ke.getKeyCode() == ke.VK_UP) && vsb!=null){

vsb.setValue(vsb.getValue() - Simage.getHeight() / 20);

AdjustmentEvent e = new


AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, AdjustmentEvent.UNIT_INCREMENT, 10);


else if((ke.getKeyCode() == ke.VK_DOWN) && vsb!=null){

vsb.setValue(vsb.getValue() + Simage.getHeight() / 20);

vsb.setValue(vsb.getValue() + 10);

AdjustmentEvent e = new AdjustmentEvent(vsb,


AdjustmentEvent.UNIT_DECREMENT, 10);


}//end method

The keyPressed method checks that the scrollbars are not null and fires the necessary adjustment events to perform scrolling. You should also update your zooming code to reset vsb and hsb to null when zoom = 0. You could also extend this functionality to horizontal scrolling as well, by handle the VK_RIGHT and VK_LEFT KeyCodes.

Learn More

In addition to our Tech Tips section, our document and image Solutions Section provides business application and industry-specific examples.


Online Demo