Skip to content

Customizing Key Handling on PdfViewer

Takashi Kawasaki edited this page Apr 29, 2025 · 1 revision

Because PdfViewer handles certain keys to allow users to scroll/zoom PDF view by keyboards, it may sometimes interfere with other widget's key handling, such as TextField's text input.

To customize the key handling behavior, you can use PdfViewerParams.onKey and PdfViewerParams.keyHandlerParams.

Default implementation

By default, PdfViewer handles the following keys:

Key Description
pageUp Scroll up
pageDown Scroll up
home Go to first page
end go to last page
equal Combination with /Ctrl to zoom up (currently not I18N-ed)
minus Combination with /Ctrl to zoom down
arrowUp Scroll upward
arrowDown Scroll downward
arrowLeft Scroll to left
arrowRight Scroll to right

And, the other keys are not handled and handled by other widgets.

Overriding the default implementation

The following fragment illustrates how to use PdfViewerParams.onKey:

onKey: (params, key, isRealKeyPress) {
  if (key == LogicalKeyboardKey.space) {
    // handling the key inside the function
    handleSpace();
    return true;
  }
  if (key == LogicalKeyboardKey.arrowLeft || key == LogicalKeyboardKey.arrowRight) {
    // returning false to disable the default logic
    return false;
  }
  // returning null to let the default logic to handle the keys
  return null;
},

Clone this wiki locally