--- title: ユーザ入力とコントロール slug: Web/Guide/User_input_methods tags: - Screen Orientation - contenteditable - drag and drop - fullscreen - keyboard - mouse - pointer lock - touch - user input translation_of: Web/Guide/User_input_methods ---

現代のウェブのユーザー入力は、単純なマウスやキーボードだけではありません。この記事では、ユーザー入力を管理し、オープンなウェブアプリに制御を実装するための推奨事項を、FAQ、実例、および基礎となる技術について、より詳細な情報を必要とする人のための詳細な情報へのリンクとともに提供します。関連する API とイベントには、タッチイベントPointer Lock APIScreen Orientation APIFullscreen APIドラッグ&ドロップ API などがあります。

ユーザー入力とコントロールのワークフロー

次の図式は、ユーザー入力の仕組みを実装するための典型的なワークフローを説明しています。

最初に、マウス、キーボード、指でのタッチなどから、アプリケーションで対象としたい入力の仕組みをどれにするかを決める必要があります。入力の仕組みを決めたのであれば、ウェブプラットフォームや JavaScript ライブラリーによって提供されているツールを使い、制御することができます。

推奨事項

利用できる入力の仕組みはアプリを動かしているデバイスの性能に依存します。

以下は推奨事項一式であり、オープンなウェブアプリでそのようなツールを利用するためのベストプラクティスです。

使用する入力の仕組みを決める

キーボード

キーボード入力はあなたのアプリによって制御できます。例えば、何らかのキーが押された時に制御を追加したい場合、ウィンドウオブジェクトにイベントリスナーを追加する必要があります。

window.addEventListener("keydown", handleKeyDown, true);
window.addEventListener("keyup", handleKeyUp, true);

handleKeyDownhandleKeyUp は、keydownkeyup イベントについての制御を実装する関数です。

: キーボードイベントについて、より知りたい人はイベントリファレンス と {{domxref("KeyboardEvent")}} ガイドをご確認ください。

マウス

ユーザーがマウスのようなポインティングデバイスと関わっている時に発生するイベントは {{domxref("MouseEvent")}} DOM インターフェースによって表されます。一般的なマウスイベントは、click イベントdblclick イベントmouseup イベント、そして mousedown イベントを含みます。マウスイベントインターフェースが使用している全てのイベントの一覧は、イベントリファレンスに記載されています。

When the input device is a mouse, you can also control user input through the Pointer Lock API and implement Drag & Drop (see below).

入力デバイスがマウスの場合、ユーザー入力を Pointer Lock API やドラッグ&ドロップ API の実装でも制御できます (下記を参照してください)。

指でのタッチ

When developing web applications meant to be installed on touchscreen devices, it’s a good practice to take into consideration the different capabilities in terms of screen resolution and user input. Touch events can help you implement interactive elements and common interaction gestures on touchscreen devices.

If you want to use touch events, you need to add event listeners and specify handler functions, which will be called when the event gets fired:

element.addEventListener("touchstart", handleStart, false);
element.addEventListener("touchcancel", handleCancel, false);
element.addEventListener("touchend", handleEnd, false);
element.addEventListener("touchmove", handleMove, false);

where element is the DOM element you want to register the touch events on.

: For further information about what you can do with touch events, please read our touch events guide.

ポインターイベント

When dealing with devices that incorporate multiple forms of input, like mouse, finger touch and pen input, it might be hard to develop a solution that works for all these different control mechanisms. Pointer Events help developers more easily manage events across devices by normalizing the handling of each one. A pointer can be any point of contact on the screen made by a mouse cursor, pen, touch (including multi-touch), or other pointing input device. The events for handling generic pointer input look a lot like those for mouse: pointerdown, pointermove, pointerup, pointerover, pointerout, etc.

: Pointer Events are not widely supported yet, but a pointer.js polyfill is available on Mozilla Github.

コントロールの実装

ポインターロック

In some cases, typically game development, you might need to access mouse events even when the cursor goes past the boundary of the browser or screen: the {{domxref("Pointer_Lock_API")}} gives you full control of the pointing device.

This is the code to request pointer lock on an element:

element.requestPointerLock();

: For a full tutorial and reference, read our {{domxref("Pointer_Lock_API")}} page.

画面の回転

When screen orientation matters for your application, you can read the screen orientation state, be informed when this state changes, and able to lock the screen orientation to a specific state (usually portrait or landscape) through the Screen Orientation API.

Orientation data can be retrieved through the {{domxref("screen.orientation")}} attribute or through the orientation media feature. When screen.orientation changes, the {{domxref("screen.orientationchange")}} event is fired on the screen object. Locking the screen orientation is made possible by invoking the {{domxref("screen.lockOrientation")}} method, while the {{domxref("screen.unlockOrientation")}} method removes all the previous screen locks that have been set.

: More information about the Screen Orientation API can be found in Managing screen orientation.

全画面

You might need to present an element of your application (such as a {{ htmlelement("video") }}, for example) in fullscreen mode. You can achieve this by calling {{domxref("Element.requestFullscreen()")}} on that element. Bear in mind that many browsers still implement this with a vendor prefix, so you will probably need to fork your code something like this:

var elem = document.getElementById("myvideo");
if (elem.requestFullscreen) {
  elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
  elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
  elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
  elem.webkitRequestFullscreen();
}

: To find more out about adding fullscreen functionality your application, read our documentation about using fullscreen mode.

ドラッグ&ドロップ

Drag & Drop allows your application’s users to click and hold the mouse button down over an element, drag it to another location, and release the mouse button to drop the element there.

Here is an example that allows a section of content to be dragged.

<div draggable="true" ondragstart="event.dataTransfer.setData('text/plain', 'This text may be dragged')">
    This text <strong>may</strong> be dragged.

</div>

in which we:

: You can find more information in the MDN Drag & Drop documentation.

コンテンツを編集可能にする

contenteditable 属性を使うことで、開いているウェブアプリのあらゆる DOM 要素を直接編集することができます。

<div contenteditable="true">
    このテキストは閲覧者が編集することができます。
</div>

: 互換性や例、その他リソースに関する情報は コンテンツを編集可能にするガイドで確認することができます。

Tracking multiple touch points at a time
This example tracks multiple touch points at a time, allowing the user to draw in a {{htmlelement("canvas")}} with more than one finger at a time. It will only work on a browser that supports touch events.
Simple pointer lock demo
We've written a simple pointer lock demo to show you how to use it to set up a simple control system. The demo uses JavaScript to draw a ball inside a {{htmlelement("canvas")}} element. When you click the canvas, pointer lock is then used to remove the mouse pointer and allow you to move the ball directly using the mouse.
コンテンツを編集可能にするデモ
このデモは、編集可能なドキュメントセクションを作成することに利用できる contenteditable がどのように動くか表示しており、その状態はその後 ローカルストレージを使い保存されます。

チュートリアル

関連情報