- Home /
how to assign a key to the scrollbar
Can someone tell me if it is possible to assign a keyboard key to use on the scrollbar to scroll up or down instead of using my mouse scroll wheel?
Are you referring to the scrollbars of the GUI, like GUI.VerticalScrollbar?
yes Veehmot That is what i´m talking about,it is possible?
Answer by Veehmot · Jun 06, 2013 at 09:16 PM
You should change the value of the Scrollbar using an input function. For example, to change the scrollbar via Input in the example of GUI.VerticalScrollbar, you should add this to the Update function.
void Update() {
if (Input.GetKeyDown(KeyCode.DownArrow)) {
vSbarValue += 1f;
} else if (Input.GetKeyDown(KeyCode.UpArrow)) {
vSbarValue -= 1f;
}
}
Or with GUI.BeginScrollView as in your example:
void Update() {
if (Input.GetKeyDown(KeyCode.DownArrow)) {
scrollPosition.y += 1f;
} else if (Input.GetKeyDown(KeyCode.UpArrow)) {
scrollPosition.y -= 1f;
}
}
for example if I got this script
using UnityEngine; using System.Collections;
public class SkinTest : $$anonymous$$onoBehaviour
{
public GUISkin thisAmigaGUISkin;
public Vector2 scrollPosition = Vector2.zero;
void OnGUI() {
scrollPosition = GUI.BeginScrollView(new Rect(10, 10, 240, 300), scrollPosition, new Rect(0, 0, 220, 500));
GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
GUI.EndScrollView();
}
}
I have updated the answer to include your example as well.
hey Veehmot i have one more question, guess what i make it work as you told me, but in my project i got more than 5 GUI.BeginScrollViews, and i did apply what you just told me to each of them, but when i scroll one, all of them scrolls together, is there a way to use the same keys but make it move separately?
Your answer
![](https://koobas.hobune.stream/wayback/20220613103557im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i change controller from keyboard to mouse?? 1 Answer
Unity UI Mouse + Keyboard navigate, Un-Highlight button choice on mouse over 0 Answers
Unity 4.6.3f1 bug or normal behaviour? MouseButtonDown cancelled/ ignored by key press 0 Answers
How to scrolling scrolling panel using Script or Keyboard 1 Answer