# \[REQUEST\] MidiKeyboardComponent::getKeyboardWidth()

**URL:** <https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815>\
**Category:** General JUCE discussion\
**Created:** [March 1, 2016, 12:11am UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815 "2016-03-01T00:11:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [March 1, 2016, 12:11am UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/1 "2016-03-01T00:11:16Z")

</div>

MidiKeyboardComponent has a **getKeyWidth** () but not a **getKeyboardWidth** () -- which means that the user needs to calculate the number of white keys and multiply by getKeyWidth() in order to set the component's bounds, which is a bit fiddly.

π

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 1, 2016, 9:28am UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/2 "2016-03-01T09:28:17Z")

</div>

Well no, that wouldn't work. But you'd just need to call&nbsp;getKeyPosition (getRangeEnd())&nbsp;to find the right-hand edge of the highest note shown. I could add a helper function that does the same thing, but in the meantime that's a pretty easy thing to do.

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [March 1, 2016, 10:42pm UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/3 "2016-03-01T22:42:00Z")

</div>

hmm It seems to work...

```
void resized() override
    {
        int **w** = (int)keyboardComponent. **getKeyWidth** () * (7 * 7 + 3), **h** = 50;
        keyboardComponent. **setSize** ( **w** , **h** );
        keyboardComponent.setCentrePosition(getWidth() / 2, getHeight() / 2);
    }
```

 ![](http://ibin.co/2YnNQj093Lse)

With your suggested workaround, I need:

```
int k = keyboardComponent **.getRangeEnd** (),
            w = keyboardComponent. **getKeyStartPosition** ( **k+2** ), **h** = 50;
        keyboardComponent.setSize( **w** , **h** );
```

It seems odd that I need k+2 not k+1.

π

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 2, 2016, 8:05am UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/4 "2016-03-02T08:05:40Z")

</div>

I already added a&nbsp;getTotalKeyboardWidth() method

---

<div class="post-metadata">

**Author:** ![\_pi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/_pi/32/527_2.png) [@\_pi](https://forum.juce.com/u/_pi)\
**Post date:** [March 2, 2016, 12:55pm UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/5 "2016-03-02T12:55:24Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [March 6, 2016, 7:52am UTC](https://forum.juce.com/t/request-midikeyboardcomponent-getkeyboardwidth/16815/6 "2016-03-06T07:52:29Z")

</div>

Why can't you just Component::getWidth()?&nbsp;
